Set Default Value In Subs/Functions

Press on command1 to call the sub with the parameter 5, and command2 to call the sub with no parameter. In the second case, the parameter value will be the default value, that was set to be 1.

Preparations

Add 2 Command Buttons (named Command1 and Command2)

Form Code

Private Sub Command1_Click()
    MySub (5)
End Sub

Private Sub Command2_Click()
    MySub
End Sub

Sub
MySub(Optional MyParameter As Long = 1)
    MsgBox ("The parameter value is: " & MyParameter)
End Sub

Go Back