在其他模块中调用函数时编译器出错
在Excel中的VBAmacros,我试图从另一个模块中的一个模块调用一个函数。
我能够成功地调用另一个模块中的函数…但只有当我忽略函数的返回值。
当我尝试调用另一个模块中的函数并保存返回值(MyReturnValue = Application.Run "Module2.MyFunctionInAnotherModule")
,出现编译器错误:“Expected:end of statement”。
很明显,我在这个语句的语法中遇到了一些问题,但是我一直无法find正确的语法。
模块1:
Public Sub WhatGives() Dim MyReturnValue As String ' Calling a subroutine in another module works Application.Run "Module2.MySub" MyReturnValue = MyFunctionInThisModule MsgBox ("MyFunctionInThisModule( ) returned: " & MyReturnValue) ' Calling a function in another module works if ' I discard the return value of the function Application.Run "Module2.MyFunctionInAnotherModule" ' But calling a function and saving its return ' value doesn't work. When I uncomment the following ' statements, the second one results in the ' compiler error: "Expected: end of statement" 'Dim MyReturnValue As String 'MyReturnValue = Application.Run "Module2.MyFunctionInAnotherModule" 'MsgBox("MyFunctionInAnotherModule( ) returned: " & MyReturnValue) End Sub Private Function MyFunctionInThisModule() MsgBox ("MyFunctionInThisModule() invoked") MyFunctionInThisModule = "Return value from MyFunctionInThisModule" End Function
模块2:
Private Sub MySub() MsgBox ("MySub( ) invoked") End Sub Private Function MyFunctionInAnotherModule() As String MsgBox ("MyFunctionInAnotherModule( ) invoked") MyFunctionInAnotherModule = "Return value from MyFunctionInAnotherModule" End Function
你需要回报你的价值。 尝试xyz = Module2.MyFunctionInAnotherModule
编辑:对不起,您还需要从您的function删除私人。
Function MyFunctionInAnotherModule() As String MsgBox ("MyFunctionInAnotherModule( ) invoked") MyFunctionInAnotherModule = "Return value from MyFunctionInAnotherModule" End Function