检测是否在 VBA 中选择了表单控件选项按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30657944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect if a Form Control option button is selected in VBA
提问by Ettelaiv
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 如果来自其他组的选项按钮为 true 怎么办?我尝试了这种语法,但收到一条错误消息:“未找到编译错误方法或数据”
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 这样方便的“右键单击 > 属性”
采纳答案by Nishank
You should remove .Valuefrom all option buttons because option buttons don't hold the resultant value, the option group control does. If you omit .Valuethen the default interface will report the option button status, as you are expecting. You should write all relevant code under commandbutton_click events because whenever the commandbutton is clicked the option button action will run.
您应该.Value从所有选项按钮中删除,因为选项按钮不保存结果值,选项组控件会保存。如果您省略,.Value则默认界面将报告选项按钮状态,正如您所期望的。您应该在 commandbutton_click 事件下编写所有相关代码,因为无论何时单击 commandbutton,选项按钮操作都会运行。
If you want to run action code when the optionbutton is clicked then don't write an if loop for that.
如果您想在单击选项按钮时运行操作代码,则不要为此编写 if 循环。
EXAMPLE:
例子:
Sub CommandButton1_Click
If OptionButton1 = true then
(action code...)
End if
End sub
Sub OptionButton1_Click
(action code...)
End sub
回答by L42
If you are using a Form Control, you can get the same property as ActiveXby using OLEFormat.Objectproperty of the Shape Object. Better yet assign it in a variable declared as OptionButtonto get the Intellisensekick in.
如果您使用的是Form Control,则可以获得与ActiveX使用 的OLEFormat.Object属性相同的属性Shape Object。最好将它分配到一个声明为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 Macroor 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.
但话又说回来,你真的不需要知道价值。
如果使用Form Control,则将一个Macro或子例程与其关联,并在选择它时执行。所以你只需要设置一个子例程来识别哪个按钮被点击,然后对其执行相应的动作。
For example you have 2 Form ControlOption Buttons.
例如,您有 2 个Form Control选项按钮。
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。

