In a VBA macro in Excel, I'm attempting to call a function from one module in another module.
I am able to successfully call a function in another module... but only if I disregard the return value from the function.
When I attempt to call a function in another module and save the return value
(MyReturnValue = Application.Run "Module2.MyFunctionInAnotherModule")
, I get a Compiler Error: "Expected: end of statement".
Clearly I'm getting something wrong in the syntax of that statement, but I have been unable to find the right syntax.
Module1:
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
Module 2:
Private Sub MySub()
MsgBox ("MySub( ) invoked")
End Sub
Private Function MyFunctionInAnotherModule() As String
MsgBox ("MyFunctionInAnotherModule( ) invoked")
MyFunctionInAnotherModule = "Return value from MyFunctionInAnotherModule"
End Function