Skip to main content

Understanding named arguments and optional arguments

When you call a Sub or Function procedure, you can supply arguments positionally, in the order that they appear in the procedure's definition, or you can supply the arguments by name without regard to position.

For example, the following Sub procedure takes three arguments.

Sub PassArgs(strName As String, intAge As Integer, dteBirth As Date)
    Debug.Print strName, intAge, dteBirth
End Sub

You can call this procedure by supplying its arguments in the correct position, each delimited by a comma, as shown in the following example.

PassArgs "Mary", 29, #2-21-69#

You can also call this procedure by supplying named arguments, delimiting each with a comma.

PassArgs intAge:=29, dteBirth:=#2/21/69#, strName:="Mary"

A named argument consists of an argument name followed by a colon and an equal sign (:=), followed by the argument value.

Named arguments are especially useful when you are calling a procedure that has optional arguments. If you use named arguments, you don't have to include commas to denote missing positional arguments. Using named arguments makes it easier to keep track of which arguments you passed and which you omitted.

Optional arguments are preceded by the Optional keyword in the procedure definition. You can also specify a default value for the optional argument in the procedure definition. For example:

Sub OptionalArgs(strState As String, Optional strCountry As String = "USA")
    . . .
End Sub

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.

The following procedure includes two optional arguments, the varRegion and varCountry variables. The IsMissing function determines whether an optional Variant argument has been passed to the procedure.

Sub OptionalArgs(strState As String, Optional varRegion As Variant, Optional varCountry As Variant = "USA")
    If IsMissing(varRegion) And IsMissing(varCountry) Then
        Debug.Print strState
    ElseIf IsMissing(varCountry) Then
        Debug.Print strState, varRegion
    ElseIf IsMissing(varRegion) Then
        Debug.Print strState, varCountry
        Debug.Print strState, varRegion, varCountry
    End If
End Sub

You can call this procedure by using named arguments as shown in the following example.

OptionalArgs varCountry:="USA", strState:="MD" 
OptionalArgs strState:= "MD", varRegion:=5

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>