Vba 调用其他模块中的函数时出现编译器错误
在Excel中的VBA宏中,我试图从一个模块调用另一个模块中的函数 我能够成功调用另一个模块中的函数。。。但前提是忽略函数的返回值 当我试图调用另一个模块中的函数并保存返回值时Vba 调用其他模块中的函数时出现编译器错误,vba,excel,office-2010,Vba,Excel,Office 2010,在Excel中的VBA宏中,我试图从一个模块调用另一个模块中的函数 我能够成功调用另一个模块中的函数。。。但前提是忽略函数的返回值 当我试图调用另一个模块中的函数并保存返回值时 (MyReturnValue=Application.Run“Module2.myfunctioninaothermodule”),我得到一个编译器错误:“预期:语句结束” 很明显,我在那句话的语法上有点错误,但我一直找不到正确的语法 模块1: Public Sub WhatGives() Dim MyReturnVal
(MyReturnValue=Application.Run“Module2.myfunctioninaothermodule”)
,我得到一个编译器错误:“预期:语句结束”
很明显,我在那句话的语法上有点错误,但我一直找不到正确的语法
模块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
Private Sub MySub()
MsgBox ("MySub( ) invoked")
End Sub
Private Function MyFunctionInAnotherModule() As String
MsgBox ("MyFunctionInAnotherModule( ) invoked")
MyFunctionInAnotherModule = "Return value from MyFunctionInAnotherModule"
End Function
模块2:
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
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.myfunction
编辑:抱歉,您还需要从函数中删除Private
Function MyFunctionInAnotherModule() As String
MsgBox ("MyFunctionInAnotherModule( ) invoked")
MyFunctionInAnotherModule = "Return value from MyFunctionInAnotherModule"
End Function
函数无法公开是有原因的吗?
MyReturnValue=Application.Run(“Module2.MyFunctionInAnotherModule”)
?感谢您的响应,Sam。不幸的是,从函数中删除“Private”并没有起作用。相同的编译器错误。很有趣。我可以毫无问题地打电话给它。您是否尝试在其他模块中使用xyz=Module2.myfunction调用它?i、 删除应用程序。运行并删除引号。