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.
The link you have provided is to a help page for VB Net and Not VBA. VBA and VB .Net are similar but have very different use cases. VBA is the built in scripting language used by Microsoft Office applications. VB Net is a full bore .Net Language which has its roots in VBA but is not used by Office Application unless you write specific VSTO addins or applications.
Optional arguments in VBA work fine. The VBA version of the code sample you provided above is.
Sub notify(ByVal company As String, Optional ByVal office As String = "QJZ")
If office = "QJZ" Then
Debug.print "office not supplied -- using Headquarters"
office = "Headquarters"
End If
' Insert code to notify headquarters or specified office.
End Sub
You could also do us all a bit of a favour and learn to use the line break so that
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)
is written as
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 _
)
You should also be aware that any optional parameters defined with a default can never be missing and that sometimes IsMissing is the better option to use because its not possible to provide a sensible default.
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