Function with optional parameter

M

mscertified

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.
 
D

Douglas J. Steele

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 = "")
 
M

mscertified

Thanks for the response.
I was able to use If len(Parm2)=0 Then ...
It worked whether the parm was present or not.
 
D

Douglas J. Steele

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.
 
Top