4

我正在尝试编写一个允许具有默认值的可选参数的 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>)可能,如果是,如何:

  1. 设置参数声明?
  2. 让它出现在查看宏列表中?

环境:

  • 字 2016 x64
  • 视窗 10

†</sup> 所有参考资料,包括与 VBA 可选参数相关的 SO 答案均来自 2015 年。

4

2 回答 2

4

您提供的链接是指向 VB Net 而不是 VBA 的帮助页面。VBA 和 VB .Net 很相似,但有非常不同的用例。VBA 是 Microsoft Office 应用程序使用的内置脚本语言。VB Net 是一种完整的 .Net 语言,它起源于 VBA,但除非您编写特定的 VSTO 插件或应用程序,否则 Office 应用程序不会使用它。

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 是更好的选择,因为它无法提供合理的默认值。

于 2020-05-08T08:03:28.730 回答
4

可选值参数声明

是的,具有默认值的可选参数直到 Word 2016 仍然有效。VBA 参考指出:

arglist 参数具有以下语法和部分:

[ 可选 ] [ ByVal | ByRef ] [ ParamArray ] varname [ ( ) ] [ As type ] [ = defaultvalue ]

可选 可选。表示不需要参数的关键字。如果使用,则 arglist 中的所有后续参数也必须是可选的,并使用Optional关键字声明。如果使用ParamArray ,则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

使用默认值声明参数时的意外接口行为是子将停止出现在宏列表中

注意子示例 1、3 所示。 2、4、5没有。

这不应被解释为子有问题或无法使用。

  • 可以理解,这似乎是一个设计决定,需要从另一个子或立即窗口1调用带有参数的子 ,因此不会显示在宏列表中 在此处输入图像描述
  • 从逻辑上讲,只有/所有可选参数(并且没有默认值†</sup>)的子程序确实在宏列表中显示2,因为根据定义,调用不需要提供显式参数
  • †</sup>不一致的是,带有可选参数提供的默认值的子不会显示在宏列表中

调用选项

调用包含默认值参数的 subs 的两个选项是:

  1. 声明一个无参数调用子,它调用有问题的子。这个调用子将出现在宏列表中。
    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

来电子显示在宏列表中

  1. 使用立即窗口1调用有问题的子

1开发人员 - Visual Basic - 视图 - 立即窗口 (Ctrl+G)

2与对与该主题相关的多个 SO 帖子的各种评论相反

于 2020-05-10T14:52:25.460 回答