Function with optional parameter

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I code a function with an optional parameter.
Also, in the function's code how do I test for the presence or absence of
the parameter?

Thanks.
 
Function MyFunc(Parm1 As String, Optional Parm2 As Variant)

If IsMissing(Parm2) Then
' nothing was passed
End If

End Function

Alternatively, if you're going to always treat the optional parameter as a
fixed value if it isn't set, you can use:

Function MyFunc(Parm1 As String, Optional Parm2 As String = "")
 
Thanks for the response.
I was able to use If len(Parm2)=0 Then ...
It worked whether the parm was present or not.
 
If that works for you, then you obviously chose the second declaration. It
definitely wouldn't work if Parm2 is declared as Variant and you don't pass
a value.
 
Back
Top