检测是否在VBA中选择了“表单控制”选项按钮

时间:2022-05-12 02:27:20

I have a code that work just fine using ActiveX option buttons. However, I want the macro to run on a Mac as well so I am trying to replace my ActiveX controls with form controls. With ActiveX, all I had to do in order to check if one of my two option buttons was selected is:

我有一个使用ActiveX选项按钮可以正常工作的代码。但是,我希望宏也能在Mac上运行,所以我试图用表单控件替换我的ActiveX控件。使用ActiveX,我只需要检查我的两个选项按钮之一是否被选中:

    Sub OptionButton1_Click
    If OptionButton1.Value = true then
        (action...)
    End if
    End sub

I have been trying to find an equivalent for Form Controls on Google but each time I get an:

我一直在尝试在Google上找到Form Controls的等价物,但每次我得到一个:

Object required error

对象必需的错误

Thank you very much for your answers @L42 and @Sai Nishank! Now what if I want to check in an OptionButton_Click if an option button from an other group is true ? I tried this syntax but I get an error message : "Compile error Method or Data not found"

非常感谢你的答案@ L42和@Sai Nishank!现在如果我想检查OptionButton_Click,如果其他组中的选项按钮是真的,该怎么办?我尝试了这种语法,但我收到一条错误消息:“编译错误方法或数据未找到”

    Sub USDButton_Click()
    MsgBox "USD"
    If Sheet1.BTUButton = True Then
    (action1)
    End If
     If Sheet1.kWhButton = True Then
    (action2)
     End If

I am not sure if BTUButton is the correct name of the button, but I don't where to check, form controls don't have that handy "Right Click > Properties" like ActiveX

我不确定BTUButton是否是正确的按钮名称,但我不在哪里检查,表单控件没有那么方便的“右键单击>属性”,如ActiveX

2 个解决方案

#1


1  

strong text

强文

You should remove .value from option button, because option buttons are meant for either true or false. So there is no need to mention .value and try this it will work now. and you shld write all there under commanbutton_click. because when ever the commandbutton is clicked the option button action will run

您应该从选项按钮中删除.value,因为选项按钮用于true或false。因此,没有必要提及.value并尝试它现在可以工作。并且你在commanbutton_click下写下所有内容。因为当单击命令按钮时,将运行选项按钮操作

And if you want o run action when the optionbutton is clicked then dont write if loop for that.

如果你想在单击选项按钮时运行动作,那么不要写if循环。

Sub CommandButton1_Click
    If OptionButton1 = true then
     (action...)
    End if
    End sub

Sub OptionButton1_Click 

(action...)
End sub

#2


8  

If you are using a Form Control, you can get the same property as ActiveX by using OLEFormat.Object property of the Shape Object. Better yet assign it in a variable declared as OptionButton to get the Intellisense kick in.

如果使用表单控件,则可以使用Shape对象的OLEFormat.Object属性获取与ActiveX相同的属性。更好的是将它分配给声明为OptionButton的变量以获得Intellisense启动。

Dim opt As OptionButton

With Sheets("Sheet1") ' Try to be always explicit
    Set opt = .Shapes("Option Button 1").OLEFormat.Object ' Form Control
    Debug.Pring opt.Value ' returns 1 (true) or -4146 (false)
End With

But then again, you really don't need to know the value.
If you use Form Control, you associate a Macro or sub routine with it which is executed when it is selected. So you just need to set up a sub routine that identifies which button is clicked and then execute a corresponding action for it.

但话说回来,你真的不需要知道它的价值。如果使用表单控件,则将宏或子例程与选择它时执行的例程相关联。因此,您只需设置一个子例程来识别单击哪个按钮,然后为其执行相应的操作。

For example you have 2 Form Control Option Buttons.

例如,您有2个表单控件选项按钮。

Sub CheckOptions()
    Select Case Application.Caller
    Case "Option Button 1"
    ' Action for option button 1
    Case "Option Button 2"
    ' Action for option button 2
    End Select
End Sub

In above code, you have only one sub routine assigned to both option buttons.
Then you test which called the sub routine by checking Application.Caller.
This way, no need to check whether the option button value is true or false.

在上面的代码中,只有一个子例程分配给两个选项按钮。然后通过检查Application.Caller测试哪个调用子例程。这样,无需检查选项按钮值是true还是false。

#1


1  

strong text

强文

You should remove .value from option button, because option buttons are meant for either true or false. So there is no need to mention .value and try this it will work now. and you shld write all there under commanbutton_click. because when ever the commandbutton is clicked the option button action will run

您应该从选项按钮中删除.value,因为选项按钮用于true或false。因此,没有必要提及.value并尝试它现在可以工作。并且你在commanbutton_click下写下所有内容。因为当单击命令按钮时,将运行选项按钮操作

And if you want o run action when the optionbutton is clicked then dont write if loop for that.

如果你想在单击选项按钮时运行动作,那么不要写if循环。

Sub CommandButton1_Click
    If OptionButton1 = true then
     (action...)
    End if
    End sub

Sub OptionButton1_Click 

(action...)
End sub

#2


8  

If you are using a Form Control, you can get the same property as ActiveX by using OLEFormat.Object property of the Shape Object. Better yet assign it in a variable declared as OptionButton to get the Intellisense kick in.

如果使用表单控件,则可以使用Shape对象的OLEFormat.Object属性获取与ActiveX相同的属性。更好的是将它分配给声明为OptionButton的变量以获得Intellisense启动。

Dim opt As OptionButton

With Sheets("Sheet1") ' Try to be always explicit
    Set opt = .Shapes("Option Button 1").OLEFormat.Object ' Form Control
    Debug.Pring opt.Value ' returns 1 (true) or -4146 (false)
End With

But then again, you really don't need to know the value.
If you use Form Control, you associate a Macro or sub routine with it which is executed when it is selected. So you just need to set up a sub routine that identifies which button is clicked and then execute a corresponding action for it.

但话说回来,你真的不需要知道它的价值。如果使用表单控件,则将宏或子例程与选择它时执行的例程相关联。因此,您只需设置一个子例程来识别单击哪个按钮,然后为其执行相应的操作。

For example you have 2 Form Control Option Buttons.

例如,您有2个表单控件选项按钮。

Sub CheckOptions()
    Select Case Application.Caller
    Case "Option Button 1"
    ' Action for option button 1
    Case "Option Button 2"
    ' Action for option button 2
    End Select
End Sub

In above code, you have only one sub routine assigned to both option buttons.
Then you test which called the sub routine by checking Application.Caller.
This way, no need to check whether the option button value is true or false.

在上面的代码中,只有一个子例程分配给两个选项按钮。然后通过检查Application.Caller测试哪个调用子例程。这样,无需检查选项按钮值是true还是false。