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.


Add 2 Command Buttons (named Command1 and Command2)

Form Code

Private Sub Command1_Click()
    MySub (5)
End Sub

Private Sub Command2_Click()
End Sub

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

Go Back