我正在尝试编写一个允许具有默认值的可选参数的 VBA 子程序。我尝试了Microsoft Docs - Optional Parameters (Visual Basic)中的示例代码,但它导致 sub 未显示在可用 sub 列表中(即来自 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
我尝试过但无法出现的子声明是:
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)
现在,我不得不用IsMissing 逻辑解决以下问题:
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)
是否(仍然†</sup>)可能,如果是,如何:
- 设置参数声明?
- 让它出现在查看宏列表中?
环境:
- 字 2016 x64
- 视窗 10
†</sup> 所有参考资料,包括与 VBA 可选参数相关的 SO 答案均来自 2015 年。