我正在尝试编写一个VBA子程序,该子程序允许可选参数具有默认值。我尝试了来自Microsoft Docs - Optional Parameters (Visual Basic)的示例代码,但它导致该子项未显示在可用子项列表中(例如,来自“查看宏”)。
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 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)
是否(仍然†)可行,如果可行,如何:
环境:
†所有参考,包括与VBA可选参数有关的SO答案,均来自2015年。
您提供的链接是VB Net而不是VBA的帮助页面。 VBA和VB .Net相似,但用例却大不相同。 VBA是Microsoft Office应用程序使用的内置脚本语言。 VB Net是一种全面的.Net语言,其源于VBA,但除非编写特定的VSTO加载项或应用程序,否则Office Application不会使用它。
VBA中的可选参数可以正常工作。您上面提供的代码示例的VBA版本是。
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
您也可以帮我们一点忙,并学会使用换行符,以便如此
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)
写为
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是更好的选择,因为它不可能提供合理的默认值。
是,具有默认值的可选参数仍然可以在Word 2016之前使用。VBA reference指出:
arglist参数具有以下语法和部分:
[可选] [ByVal | ByRef] [ParamArray]变量名[()] [作为类型] [=默认值]
可选可选。 Keyword指示不需要参数。如果使用,则arglist中的所有后续参数也必须是可选的,并使用Optional关键字进行声明。如果使用ParamArray,则不能将Optional用作任何参数。
defaultvalue可选。任何常量或常量表达式。仅对Optional参数有效。如果类型是Object,则显式默认值只能是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
声明具有默认值的参数时的意外接口行为是子将停止出现在“宏”列表中。
不应将其解释为潜艇有问题或无法使用。
用于调用包含默认值的参数的子的两个选项是:
声明一个无参数调用子,它调用有问题的子。 此调用子将出现在宏列表中。
Sub OptParam_Test_4(可选参数As String =“ hello”)'不显示在宏列表中Debug.Print(param)'输出:你好结束子
Sub OptParam_Test_4_()'显示在宏列表中OptParam_Test_4'输出:你好结束子
[1 Developer-Visual Basic-视图-立即窗口(Ctrl + G)
[2与关于与该主题相关的多个SO帖子的各种评论相反