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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top