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
To indicate optional arguments, Square brackets are commonly used, and can also be used to group parameters that must be specified together. To indicate required arguments, Angled brackets are commonly used, following the same grouping conventions as square brackets.
When you call a procedure with an optional argument, you can choose whether or not to specify the optional argument. If you don't specify the optional argument, the default value, if any, is used. If no default value is specified, the argument would be for any variable of the specified type.
You can assign an optional argument using the assignment operator in a function definition or using the Python **kwargs statement. There are two types of arguments a Python function can accept: positional and optional. Optional arguments are values that do not need to be specified for a function to be called.
To correct this error If they are not, either supply the argument in the call, or declare the parameter Optional in the definition.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With