IsMissing 函数IsMissing function

返回一个布尔值, 该值指示是否已将可选Variant 参数传递给过程Returns a Boolean value indicating whether an optional Variant argument has been passed to a procedure.



必需的 argname 参数包含可选 Variant 过程参数的名称。The required argname argument contains the name of an optional Variant procedure argument.


使用 IsMissing 函数可检测调用过程时是否提供了可选 Variant 参数。Use the IsMissing function to detect whether or not optional Variant arguments have been provided in calling a procedure. 如果未为指定参数传递任何值,则 IsMissing 将返回 True;否则将返回 FalseIsMissing returns True if no value has been passed for the specified argument; otherwise, it returns False.

如果 IsMissing 针对某参数返回 True,则在其他代码中使用缺少参数可能导致用户定义的错误。If IsMissing returns True for an argument, use of the missing argument in other code may cause a user-defined error.

如果对 ParamArray 参数使用 IsMissing,则将始终返回 FalseIf IsMissing is used on a ParamArray argument, it always returns False. 若要检测空 ParamArray,请测试以查看数组的上限是否低于下限。To detect an empty ParamArray, test to see if the array's upper bound is less than its lower bound.

IsMissing 对简单数据类型(如 IntegerDouble)无效,因为与 Variants 不同,它们没有针对“缺少”标志位的设置。IsMissing does not work on simple data types (such as Integer or Double) because, unlike Variants, they don't have a provision for a "missing" flag bit. 为此,键入的可选参数的语法允许您指定默认值。Because of this, the syntax for typed optional arguments allows you to specify a default value. 如果在调用过程时省略该参数, 则该参数将具有此默认值, 如以下示例中所示。If the argument is omitted when the procedure is called, the argument will have this default value, as in the following example.

    Sub MySub(Optional MyVar As String = "specialvalue")
        If MyVar = "specialvalue" Then
            ' MyVar was omitted.
    End Sub

在很多情况下, 您If MyVar可以通过将默认值设置为等于您要MyVar包含的值 (如果用户省略函数调用中的值) 来完全省略测试。In many cases you can omit the If MyVar test entirely by making the default value equal to the value you want MyVar to contain if the user omits it from the function call. 这使您的代码简洁高效。This makes your code more concise and efficient.


此示例使用 IsMissing 函数检查是否已将可选参数传递到用户定义的过程。This example uses the IsMissing function to check if an optional argument has been passed to a user-defined procedure. 请注意, Optional 参数现在可具有默认值和 Variant 之外的类型。Note that Optional arguments can now have default values and types other than Variant.

Dim ReturnValue
' The following statements call the user-defined function procedure.
ReturnValue = ReturnTwice()    ' Returns Null.
ReturnValue = ReturnTwice(2)    ' Returns 4.

' Function procedure definition.
Function ReturnTwice(Optional A)
    If IsMissing(A) Then
        ' If argument is missing, return a Null.
        ReturnTwice = Null
        ' If argument is present, return twice the value.
        ReturnTwice = A * 2
    End If
End Function

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.