为什么我不能在VBA中向OptionButton发送参数

时间:2021-07-31 21:02:52

I have the following function in VBA:

我在VBA中有以下功能:

Private Function Option1Checked(option1 As OptionButton) As Integer
    option1.ForeColor = vbGreen
    If (option1.Value = True) Then
        Option1Checked = 1
    End If
    Option1Checked = 0
End Function

Whenever I try to call the function like this

每当我尝试这样调用函数时

counter = counter + Option1Checked(OptionButton1)

I get a type mismatch error at runtime. But OptionButton1 is OptionButton, so what am I doing wrong?

我在运行时遇到类型不匹配错误。但是OptionButton1是OptionButton,所以我做错了什么?

1 个解决方案

#1


4  

You're running into one of the 'features' of VBA here. If you refer to some objects, like the option button, without a property specified, VBA assumes you want the default property, not the object itself. In the case of the option button, the default property is .Value, so in your code, OptionButton1 is not the option button object, but rather TRUE or FALSE depending on whether or not the OptionButton1 is checked.

你在这里遇到了VBA的一个“功能”。如果引用某些对象(如选项按钮)而未指定属性,则VBA假定您需要默认属性,而不是对象本身。对于选项按钮,默认属性为.Value,因此在您的代码中,OptionButton1不是选项按钮对象,而是TRUE或FALSE,具体取决于是否选中了OptionButton1。

Your best bet will be to change your function to this:

您最好的选择是将功能更改为:

Private Function Option1Checked(option1 As Boolean) As Integer
    //option1.ForeColor = vbGreen
    If (option1 = True) Then
        Option1Checked = 1
    Else
        Option1Checked = 0
    End If
End Function

The downside here is that you cannot change the foreground color of the option button to green without referring to it by name.

这里的缺点是你不能将选项按钮的前景色更改为绿色,而不是按名称引用它。

An alternative that would get you the functionality that you want would be to pass the name of the option button to your Function.

可以为您提供所需功能的替代方法是将选项按钮的名称传递给您的函数。

Private Function Option1Checked(ByVal option1 As String) As Integer
    UserForm1.Controls(option1).ForeColor = vbGreen
    If (UserForm1.Controls(option1) = True) Then
        Option1Checked = 1
    Else
        Option1Checked = 0
    End If
End Function

Sub MyCountingRoutine()
    Dim str As String
    str = OptionButton1.Name

   counter = counter + Option1Checked(str)
End Sub

Make sure you include the Else in the If..Then statement in your function, otherwise you will always get 0 back.

确保在函数的If..Then语句中包含Else,否则将始终返回0。

#1


4  

You're running into one of the 'features' of VBA here. If you refer to some objects, like the option button, without a property specified, VBA assumes you want the default property, not the object itself. In the case of the option button, the default property is .Value, so in your code, OptionButton1 is not the option button object, but rather TRUE or FALSE depending on whether or not the OptionButton1 is checked.

你在这里遇到了VBA的一个“功能”。如果引用某些对象(如选项按钮)而未指定属性,则VBA假定您需要默认属性,而不是对象本身。对于选项按钮,默认属性为.Value,因此在您的代码中,OptionButton1不是选项按钮对象,而是TRUE或FALSE,具体取决于是否选中了OptionButton1。

Your best bet will be to change your function to this:

您最好的选择是将功能更改为:

Private Function Option1Checked(option1 As Boolean) As Integer
    //option1.ForeColor = vbGreen
    If (option1 = True) Then
        Option1Checked = 1
    Else
        Option1Checked = 0
    End If
End Function

The downside here is that you cannot change the foreground color of the option button to green without referring to it by name.

这里的缺点是你不能将选项按钮的前景色更改为绿色,而不是按名称引用它。

An alternative that would get you the functionality that you want would be to pass the name of the option button to your Function.

可以为您提供所需功能的替代方法是将选项按钮的名称传递给您的函数。

Private Function Option1Checked(ByVal option1 As String) As Integer
    UserForm1.Controls(option1).ForeColor = vbGreen
    If (UserForm1.Controls(option1) = True) Then
        Option1Checked = 1
    Else
        Option1Checked = 0
    End If
End Function

Sub MyCountingRoutine()
    Dim str As String
    str = OptionButton1.Name

   counter = counter + Option1Checked(str)
End Sub

Make sure you include the Else in the If..Then statement in your function, otherwise you will always get 0 back.

确保在函数的If..Then语句中包含Else,否则将始终返回0。