I am trying to write a VBA sub that allows optional parameters with default values. I tried the sample code from Microsoft Docs - Optional Parameters (Visual Basic) but it leads to the sub not showing on list of available subs (i.e. from View Macros).
Sub notify(ByVal company As String, Optional ByVal office As String = "QJZ")
If office = "QJZ" Then
Debug.WriteLine("office not supplied -- using Headquarters")
office = "Headquarters"
End If
' Insert code to notify headquarters or specified office.
End Sub
The sub declaration I tried, but couldn't get to appear, was:
Sub HighlightByFont( _
Optional ByVal highlightFont As Variant = "", _
Optional ByVal highlightColor As Variant = "", _
Optional ByVal highlightText As Variant = "", _
Optional ByVal matchWildcards As Variant = False, _
Optional ByVal useGUI As Variant = False, _
Optional ByVal highlightBold As Variant = False, _
Optional ByVal highlightItalic As Variant = False)
For now, I have had to settle for the below with IsMissing logic:
Sub HighlightByFont( _
Optional ByVal highlightFont As Variant, _
Optional ByVal highlightColor As Variant, _
Optional ByVal highlightText As Variant, _
Optional ByVal matchWildcards As Variant, _
Optional ByVal useGUI As Variant, _
Optional ByVal highlightBold As Variant, _
Optional ByVal highlightItalic As Variant)
Is it (still†) possible, and if so, how:
Environment:
† All references, including SO answers related to VBA optional paramaters are from 2015.
Yes, optional parameters with default values still works as late as Word 2016. The VBA reference states:
The arglist argument has the following syntax and parts:
[ Optional ] [ ByVal | ByRef ] [ ParamArray ] varname [ ( ) ] [ As type ] [ = defaultvalue ]
Optional Optional. Keyword indicating that an argument is not required. If used, all subsequent arguments in arglist must also be optional and declared by using the Optional keyword. Optional can't be used for any argument if ParamArray is used.
defaultvalue Optional. Any constant or constant expression. Valid for Optional parameters only. If the type is an Object, an explicit default value can only be Nothing.
Sub OptParam_Test_1()
'Shows in Macro list
End Sub
Sub OptParam_Test_2(param)
'Does not show in Macro list
Debug.Print (param) 'Output for OptParam_Test_2 "hello": hello
End Sub
Sub OptParam_Test_3(Optional param)
'Shows in Macro list
Debug.Print (param) 'Output: hello
End Sub
Sub OptParam_Test_4(Optional param As String = "hello")
'Does not show in Macro list
Debug.Print (param) 'Output: hello
End Sub
Sub OptParam_Test_5(Optional param As Variant = "hello again")
'Does not show in Macro list
Debug.Print (param) 'Output: hello again
End Sub
An unexpected interface behavior when declaring parameters with default values is the sub will stop appearing in the Macros list.
This should not be interpreted as the sub having an issue or not being available for use.
Two options for invoking subs containing a parameter with default value are:
Sub OptParam_Test_4(Optional param As String = "hello")
'Does not show in Macro list
Debug.Print (param) 'Output: hello
End Sub
Sub OptParam_Test_4_()
'Shows in Macro list
OptParam_Test_4
'Output: hello
End Sub
1Developer - Visual Basic - View - Immediate Window (Ctrl+G)
2contrary to various comments on mulitple SO posts related to this topic