如何检查选项按钮是否被选中 [英] How to check if an Option Button is selected

查看:262
本文介绍了如何检查选项按钮是否被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要检查代码是否选择特定的选项按钮。

I want to be able to check in code whether or not a particular Option Button is selected.

我在Excel工作表中制作了几个表单控件按钮,我尝试过如下:

I made a couple of Form Control buttons in an Excel sheet and I tried something like:

if Activesheet.myButton = true then

     (do stuff)

endif

但是我得到一个对象不支持此属性或方法

But I get an "Object doesn't support this property or method" error.

使用ActiveX选项按钮会更好吗?

Would it be better to use an ActiveX Option Button?

推荐答案

默认情况下,VBA可以轻松访问放置在工作表上的表单控件所需的类型。

By default, VBA hides easy access to the types needed to work with Form Controls placed on a Worksheet.

在VBA编辑器中,按F2 '功能键或从视图菜单 - >对象浏览器。这将打开对象浏览器窗口。如果在Excel库中搜索'OptionButton',那么给定的任务就不会有任何用处。

In the VBA editor, either press the 'F2' function key or from the View Menu->Object Browser. This will open the Object Browser window. If search the Excel library for 'OptionButton', you will not find anything of use for the given task.

但是,如果您右键单击库窗口并单击显示隐藏的成员,您将获得可以使用的完整的类列表。一旦你启用了隐藏的成员,VBA的Intellisense也将向你展示这些类。

However, if you right-click in the 'Library Window' and click on 'Show Hidden Members', you will be rewarded with a complete list of Classes that you can use. Once you have enabled 'Hidden Members', VBA's Intellisense will also show these classes to you.

表单控件以Shape形式添加到工作表中,但Shape对象本身没有任何属性,可以看到是否选中了Option Button。您可以通过'OLEFormat.Object'属性访问OptionButton,但在这一点上它只是一个对象类型。
但是,您可以将对象转换为Excel.OptionButton,如以下代码所示。你会注意到,我用'Excel'命名空间(库)名称来表示'OptionButton'。通过这样做,Intellisense将在您输入。时立即启动,并为您提供可供选择的课程列表。 OptionButton类公开了一个值属性,允许您检查它是否被选中(值= 1)或未选中(值= 0)。

The Form Controls are added to the Worksheet as a Shape, but the Shape object itself does not have any property that allows you see if the Option Button is checked or not. You can access the OptionButton through the 'OLEFormat.Object' property, but at this point it is just an 'Object' type. However you can cast the Object to an Excel.OptionButton as shown in the following code. You will notice that I preface 'OptionButton' with the 'Excel' namespace (Library) name. By doing this, Intellisense will kick-in as soon as you type the '.' and provide you with a list of classes to choose from. The OptionButton class exposes a 'Value' property that allows you to check if it is checked (Value = 1) or unchecked (Value = 0).

Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Worksheets.Item("Sheet2")

Dim btn As Shape
Set btn = ws.Shapes.Item("Option Button 1")
Dim optBtn1 As Excel.OptionButton
Set optBtn1 = Sheet2.Shapes.Item("Option Button 1").OLEFormat.Object

If optBtn1.Value = 1 Then ' it is checked
   Debug.Print "Option Button 1 is checked"
End If

现在这是全部的工作,但现在您已经访问了隐藏的成员,您还可以访问存在于WorkSheet对象上的OptionButtons集合,您可以跳过以Shape形式访问它

Now this is all works, but now that you have acces to Hidden Members, you also have access to the OptionButtons collection that exists on the WorkSheet object and you can skip accessing it as a Shape Object.

Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Worksheets.Item("Sheet2")

Dim optBtn2 As Excel.OptionButton
Set optBtn2 = ws.OptionButtons.Item("Option Button 2")
If optBtn2.Value = 1 Then ' it is checked
   Debug.Print "Option Button 2 is checked"
End If

这篇关于如何检查选项按钮是否被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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