Home » excel » vba – How to call a function with arguments, return from the function and assign what was returned to a variable?

vba – How to call a function with arguments, return from the function and assign what was returned to a variable?

Posted by: admin May 14, 2020 Leave a comment

Questions:

This is what I was trying:

Calling the function from the module:

LastWorkingDay = IsBusinessDay(Yesterday)

Function:

Public Function IsBusinessDay(Yesterday As Variant) As Date

...

InterestingVariable = #20/02/2014#

End Function

I would like to return InterestingVariable and assign it to LastWorkingDay. There is no return statement in VBA, so how do you decide what you want to return. Or, how do you call a function to return a particular variable calculated in the function In my case it would “InterestingVariable”.

How to&Answers:

return in VBA is the function name and = <return value>

so for example

Public Function MyFunction() as String
    MyFunction = "returned from MyFunction"
End Function

so MsgBox MyFunction returns a String returned from MyFunction

Answer:

add this line to your function

IsBusinessDay = InterestingVariable