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

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


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.


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 IfEnd Sub


If you are using a string or number variable you can check the value of the variable. For example:

Function func (Optional Str as String, Optional Num as Integer)If Str = "" Then    MsgBox "NOT SENT"End IfIf Num = 0 Then    MsgBox "NOT SENT"End IfEnd Function

This allows you to use non-variant variables.