Alex Rivera | Logout

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

Asked 2009-11-02T11:38:43.010
87

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 
Edit
Report

2 Answers

114

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.

answered 2009-11-02T11:41:10.477
25

You can use the IsMissing() Function. But this one only works with the Variant datatype.

Sub func(Optional s As Variant)
   If IsMissing(s) Then
      ' ...
   End If
End Sub
answered 2009-11-02T11:43:57.977

Your Answer