Fairly new to VBscript and VBA... hoping for some help and that it's an easy answer...
I'm calling a Macro / Function in Excel VBA from VBscript. The call to the Function should return a number. Using VBA debug in Excel, the function appears to work properly (in this example, it displays a value of 1), but when I call the Macro / Function and attempt to echo the value in VBscript it shows as "Empty".
How can I get the value from VBA back to VBscript?
Thanks for your help
Example of VBscript code:
Set excelOBJ = CreateObject("Excel.Application")
Set workbookOBJ = excelOBJ.Workbooks.Open("C:\variable.xlsm")
excelOBJ.Application.Visible = True
excelOBJ.DisplayAlerts = False
REM mostly for testing purposes
Dim returnValue
returnValue = 10
Wscript.Echo "'returnValue' value before call to macro function = " & returnValue
Wscript.Echo "'returnValue' TypeName before call to macro function = " & TypeName(returnValue)
returnValue = excelOBJ.Run("ThisWorkbook.getNum")
Wscript.Echo "'returnValue' value after call to macro function = " & returnValue
Wscript.Echo "'returnValue' TypeName after call to macro function = " & TypeName(returnValue)
excelOBJ.quit
Example of VBA in Excel:
Public Function getNum()
getNum = 1
Debug.Print "getNum value = " & getNum
End Function
Output:
'returnValue' value before call to macro function = 10
'returnValue' TypeName before call to macro function = Integer
REM Inside Excel VBA editor
getNum value = 1
'returnValue' value after call to macro function =
'returnValue' TypeName after call to macro function = Empty
I'd recommend moving your code to a module if not there already.
This code should be changed
returnValue = excelOBJ.Run("ThisWorkbook.getNum")
If code is in a worksheet, this might work assuming your Worksheet is "Sheet1"
returnValue = excelOBJ.Run("Sheet1.getNum")
Otherwise if it's in a Module, simply use module name
returnValue = excelOBJ.Run("Module1.getNum")
If it starts running with this change but you're not getting anything returned you can change your function to pass return value parameter ByRef and check it that way
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With