簡體   English   中英

從VBA Excel宏功能在VBscript中獲取價值

[英]Getting value in VBscript from VBA Excel Macro Function

對VBscript和VBA來說還很新...希望能獲得幫助,這是一個簡單的答案...

我正在從VBscript在Excel VBA中調用宏/函數。 對該函數的調用應返回一個數字。 在Excel中使用VBA調試時,該函數似乎正常工作(在本示例中,該函數顯示為1),但是當我調用宏/函數並嘗試在VBscript中回顯該值時,該函數顯示為“ Empty”。

如何將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

如果不存在,建議將代碼移到模塊中。

該代碼應更改

returnValue = excelOBJ.Run("ThisWorkbook.getNum")

如果代碼在工作表中,則假定您的工作表為“ Sheet1”,這可能會起作用

returnValue = excelOBJ.Run("Sheet1.getNum")

否則,如果它在模塊中,則只需使用模塊名稱

returnValue = excelOBJ.Run("Module1.getNum")

如果此更改開始運行,但沒有返回任何內容,則可以更改函數以傳遞return value parameter ByRef並進行檢查

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM