Why I can not send OptionButton parameter in VBA

I have the following function in 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 a 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?

+3
source share
1 answer

Here you use one of the "functions" of VBA. If you reference some objects, such as a select button, without the specified property, VBA assumes that you want the default property, not the object itself. In the case of the select button, the default property is .Value, so in your code OptionButton1 is not the object of the option button, but rather TRUE or FALSE, depending on whether OptionButton1 is checked.

:

Private Function Option1Checked(option1 As Boolean) As Integer
    //option1.ForeColor = vbGreen
    If (option1 = True) Then
        Option1Checked = 1
    Else
        Option1Checked = 0
    End If
End 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

, Else If..Then , 0 .

+4

Source: https://habr.com/ru/post/1728216/


All Articles