检测是否在VBA中选择了“表单控件"选项按钮 [英] Detect if a Form Control option button is selected in VBA

查看:444
本文介绍了检测是否在VBA中选择了“表单控件"选项按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ActiveX选项按钮可以正常工作的代码.但是,我也希望该宏也能在Mac上运行,因此我试图将ActiveX控件替换为窗体控件.使用ActiveX,我要做的就是检查是否选中了两个选项按钮之一:

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:

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

我一直试图在Google上找到与Form Controls相同的东西,但是每次我得到一个:

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

对象必填错误

Object required error

非常感谢您的回答@ L42和@Sai Nishank!现在,如果我想签入OptionButton_Click,如果来自另一个组的选项按钮为true怎么办?我尝试使用此语法,但收到错误消息:找不到编译错误方法或数据"

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"

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

我不确定BTUButton是否是按钮的正确名称,但是我不知道要检查哪里,表单控件没有像ActiveX这样方便的右键单击>属性"

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

推荐答案

您应该从所有选项按钮中删除.Value,因为选项按钮不保存结果值,而选项组控件则保留.如果省略.Value,则默认界面将按您期望的那样报告选项按钮的状态.您应该在commandbutton_click事件下编写所有相关代码,因为每当单击commandbutton时,选项按钮动作都会运行.

You should remove .Value from all option buttons because option buttons don't hold the resultant value, the option group control does. If you omit .Value then 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.

如果要在单击选项按钮时运行操作代码,则不要为此编写if循环.

If you want to run action code when the optionbutton is clicked then don't write an if loop for that.

示例:

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

Sub OptionButton1_Click   
    (action code...)
End sub

这篇关于检测是否在VBA中选择了“表单控件"选项按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆