Search code examples
vbaoptional-arguments

Passing optional arguments "by Value" in VBA


In VBA, if I want to pass an optional argument ByVal (see mainly Var2, the other variables are only there to make sure it works within a "complex" parameter set but should work as well):

Sub Test(Var1 As String, Optional ByVal Var2 As String, Optional Var3 As String)

    'Var1 should be passed ByRef (NOT optional) (ByRef through "default behaviour")
    'Var2 should be passed ByVal (optional) (ByVal through specific notation)
    'Var3 should be passed ByRef (optional) (ByRef through "default behaviour")

    End Sub

I only found a reference for VB does it work for VBA in the same way?

Note: Of course I tested it and the test works, and others use it as well, I just want to make sure I don't run into problem with other instances.


Solution

  • Don't know what a "complex parameter set" could be, but it will work as officially documented:

    Function Statement Syntax