为什么我的函数假定缺少参数?

时间:2022-02-09 15:48:19

I have a function which updates a form, "LoadingInterface". The function looks like this:

我有一个更新表单“LoadingInterface”的函数。该函数如下所示:

Private Sub updateLoadingBar(Optional tekst As String, Optional barOnePerc As Long, Optional barTwoPerc As Long)
    If Not IsMissing(tekst) Then
        LoadingInterface.Label1.Caption = tekst
    End If
    If Not IsMissing(barOnePerc) Then
        LoadingInterface.Bar.Width = barOnePerc * 1.68
        LoadingInterface.prosent.Caption = barOnePerc & "%"
        LoadingInterface.prosent.Left = barOnePerc * 1.68 / 2 - 6
    End If
    If Not IsMissing(barTwoPerc) Then
        LoadingInterface.SubBar.Width = barTwoPerc * 1.68
    End If
    LoadingInterface.Repaint
End Sub

I then call the function like this, expecting it to only update the textfield, since the other two arguments are missing.

然后我调用这样的函数,期望它只更新文本字段,因为缺少其他两个参数。

Call updateLoadingBar(tekst:="Test")

This works fine for updating Label1, but unfortunately the other two values are updated too - it seems that not including any values in the function-call makes VBA assume the two variables values are 0. What's more, it appears that the IsMissing function does not detect that the two values are missing when the function is called, which is the bigger problem. Stepping through the code using F8 confirms that all the if-statements are indeed entered.

这适用于更新Label1,但不幸的是其他两个值也被更新 - 似乎不包括函数调用中的任何值使得VBA假设两个变量值为0.而且,似乎IsMissing函数没有在调用函数时检测到两个值都丢失了,这是一个更大的问题。使用F8逐步执行代码确认确实输入了所有if语句。

Is there any way to make the code skip the two lowermost if-statements in my function, if no values are provided for the parameters barOnePerc and barTwoPerc?

如果没有为参数barOnePerc和barTwoPerc提供值,有没有办法让代码跳过我函数中最下面的两个if语句?

1 个解决方案

#1


9  

IsMissing only works if the argument is declared as a Variant.

IsMissing仅在参数声明为Variant时才有效。

I don't think you can validly distinguish between 0 and no passed parameter for the Long. In that case you would need to declare as Variant in the signature. You can later cast if required.

我认为你不能有效地区分0和没有传递参数的Long。在这种情况下,您需要在签名中声明为Variant。如果需要,您可以稍后施放。

I guess you could put a default (unlikely number) and test for that. Note: I wouldn't advise this. This just screams "Bug".

我想你可以设置默认值(不太可能的数字)并测试。注意:我不建议这样做。这只是尖叫“Bug”。

IsMissing:

不见了:

IsMissing returns a Boolean value indicating whether an optional Variant argument has been passed to a procedure.

IsMissing返回一个布尔值,指示是否已将可选的Variant参数传递给过程。

Syntax: IsMissing(argname)

语法:IsMissing(argname)

The required argname argument contains the name of an optional Variant procedure argument.

必需的argname参数包含可选的Variant过程参数的名称。

Remarks: Use the IsMissing function to detect whether or not optional Variant arguments have been provided in calling a procedure. IsMissing returns True if no value has been passed for the specified argument; otherwise, it returns False.

备注:使用IsMissing函数检测在调用过程时是否提供了可选的Variant参数。如果没有为指定的参数传递值,则IsMissing返回True;否则,返回False。

Both methods:

两种方法:

Option Explicit

Public Sub Test()
    RetVal
    RetVal2
End Sub

Public Function RetVal(Optional ByVal num As Long = 1000000) As Long

    If num = 1000000 Then

        MsgBox "No value passed"
        RetVal = num
    Else

        MsgBox "Value passed " & num
        RetVal = num
    End If

End Function


Public Function RetVal2(Optional ByVal num As Variant) As Long

    If IsMissing(num) Then

        MsgBox "No value passed"

    Else

        MsgBox "Value passed " & num
        RetVal2 = CLng(num)
    End If

End Function

#1


9  

IsMissing only works if the argument is declared as a Variant.

IsMissing仅在参数声明为Variant时才有效。

I don't think you can validly distinguish between 0 and no passed parameter for the Long. In that case you would need to declare as Variant in the signature. You can later cast if required.

我认为你不能有效地区分0和没有传递参数的Long。在这种情况下,您需要在签名中声明为Variant。如果需要,您可以稍后施放。

I guess you could put a default (unlikely number) and test for that. Note: I wouldn't advise this. This just screams "Bug".

我想你可以设置默认值(不太可能的数字)并测试。注意:我不建议这样做。这只是尖叫“Bug”。

IsMissing:

不见了:

IsMissing returns a Boolean value indicating whether an optional Variant argument has been passed to a procedure.

IsMissing返回一个布尔值,指示是否已将可选的Variant参数传递给过程。

Syntax: IsMissing(argname)

语法:IsMissing(argname)

The required argname argument contains the name of an optional Variant procedure argument.

必需的argname参数包含可选的Variant过程参数的名称。

Remarks: Use the IsMissing function to detect whether or not optional Variant arguments have been provided in calling a procedure. IsMissing returns True if no value has been passed for the specified argument; otherwise, it returns False.

备注:使用IsMissing函数检测在调用过程时是否提供了可选的Variant参数。如果没有为指定的参数传递值,则IsMissing返回True;否则,返回False。

Both methods:

两种方法:

Option Explicit

Public Sub Test()
    RetVal
    RetVal2
End Sub

Public Function RetVal(Optional ByVal num As Long = 1000000) As Long

    If num = 1000000 Then

        MsgBox "No value passed"
        RetVal = num
    Else

        MsgBox "Value passed " & num
        RetVal = num
    End If

End Function


Public Function RetVal2(Optional ByVal num As Variant) As Long

    If IsMissing(num) Then

        MsgBox "No value passed"

    Else

        MsgBox "Value passed " & num
        RetVal2 = CLng(num)
    End If

End Function