How do I test if optional arguments are supplied or not?

Robin Rodricks picture Robin Rodricks · Nov 2, 2009 · Viewed 89.3k times · Source

How do I test if optional arguments are supplied or not? -- in VB6 / VBA

Function func (Optional ByRef arg As Variant = Nothing)

    If arg Is Nothing Then   <----- run-time error 424 "object required"
        MsgBox "NOT SENT"
    End If

End Function 

Answer

Konrad Rudolph picture Konrad Rudolph · Nov 2, 2009

Use IsMissing:

If IsMissing(arg) Then
    MsgBox "Parameter arg not passed"
End If

However, if I remember correctly, this doesn’t work when giving a default for the argument, and in any case it makes using the default argument rather redundant.