VBScript - using macro %varname% to pass values

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
mkroeger
Newbie
Posts: 4
Joined: Tue Oct 30, 2012 2:00 am

VBScript - using macro %varname% to pass values

Post by mkroeger » Thu Mar 27, 2014 3:55 pm

I have been searching for examples, but have not found any... So I shall pose the question here:

I am creating a simple piece of code that will copy values from one excel sheet to another. I have this working fine using the Excel functions(XLOpen, XLGetCell, XLSetCell, etc).

Now I am trying to convert this to using VBScript commands.

When using the Vbscript commands, the script fails on the SET:
VBEval>GetCellFrom("Setup",13,6),tempMoveData
VBEval>SetCellTo("Setup",13,6,%tempMoveData%),nulto

It appears to NOT like the %tempMoveData%. How do you pass the values of variables to the VBScript subs?

Thanks, Mike

VBSTART
Dim xlAppFrom
Dim xlBookFrom
Dim xlAppTo
Dim xlBookTo


'Opens the Excel file in Excel
Sub OpenFromExcelFile(fromfilename)
Set xlAppFrom = CreateObject("Excel.Application")
xlAppFrom.visible = true
Set xlBookFrom = xlAppFrom.Workbooks.open(fromfilename)
end sub

Sub OpenToExcelFile(tofilename)
Set xlAppTo = CreateObject("Excel.Application")
xlAppTo.visible = true
Set xlBookTo = xlAppTo.Workbooks.open(tofilename)
end sub

'Use this to close Excel later
Sub CloseFromExcel
xlAppFrom.quit
Set xlAppFrom = Nothing
End Sub

Sub CloseToExcel
xlAppTo.quit
Set xlAppTo = Nothing
End Sub

'Retrieves a cell value from the specified
'worksheet
Function GetCellFrom(Sheet,Row,Column)
Dim xlSheetFrom
Set xlSheetFrom = xlBookFrom.Worksheets(Sheet)
GetCellFrom = xlSheetFrom.Cells(Row, Column).Value
End Function

Function GetCellTo(Sheet,Row,Column)
Dim xlSheetGetTo
Set xlSheetGetTo = xlBookTo.Worksheets(Sheet)
GetCellTo = xlSheetGetTo.Cells(Row, Column).Value
End Function

'Sets specified cell of specified worksheet
Function SetCellTo(Sheet,Row,Column,NewValue)
Dim xlSheetTo
Set xlSheetTo = xlBookTo.Worksheets(Sheet)
xlSheetTo.Cells(Row,Column).Value = NewValue
End Function
VBEND

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Mar 27, 2014 4:52 pm

Looks like tempMoveData is a string so because this is a VBScript expression and VBScript expects quoted strings you need to do:

VBEval>SetCellTo("Setup",13,6,"%tempMoveData%"),nulto
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

mkroeger
Newbie
Posts: 4
Joined: Tue Oct 30, 2012 2:00 am

Post by mkroeger » Thu Mar 27, 2014 8:02 pm

Yes! Thanks that works.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts