VBscript和VBA相当新的...希望得到一些帮助,这是一个简单的答案......
我正在从VBscript调用Excel VBA中的宏/函数 . 对函数的调用应该返回一个数字 . 在Excel中使用VBA调试,该函数似乎正常工作(在此示例中,它显示值1),但是当我调用宏/函数并尝试回显VBscript中的值时,它显示为“空” .
如何将VBA中的值恢复为VBscript?
谢谢你的帮助
VBscript代码示例:
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
Excel中的VBA示例:
Public Function getNum()
getNum = 1
Debug.Print "getNum value = " & getNum
End Function
输出:
'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
1 回答
我建议将代码移动到模块中,如果不存在的话 .
应该更改此代码
如果代码在工作表中,这可能会假设您的工作表是“Sheet1”
否则,如果它在模块中,只需使用模块名称
如果它开始运行此更改,但您没有收到任何返回,您可以更改您的函数以传递
return value parameter ByRef
并检查它