是否可以选择一个复选框并取消选择另一个复选框?(需要 VBA)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9783740/
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
is it possible to select one checkbox and deselect the other checkbox? (VBA required)
提问by user1204868
I have 2 checkboxes, what i want to do is when i select one, the other 1 will be deselected, which means user will not have the ability to select it the other checkbox. I was wondering if it is possible to do so?
我有 2 个复选框,我想要做的是当我选择一个时,另一个 1 将被取消选择,这意味着用户将无法选择另一个复选框。我想知道是否有可能这样做?
回答by Siddharth Rout
Yes it is possible. But why not use an option button instead?
对的,这是可能的。但为什么不使用选项按钮呢?
Anyways to answer your query.
无论如何回答您的查询。
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
CheckBox2.Value = False
CheckBox2.Enabled = False
Else
CheckBox2.Enabled = True
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
CheckBox1.Value = False
CheckBox1.Enabled = False
Else
CheckBox1.Enabled = True
End If
End Sub
FOLLOWUP
跟进
Thanks for the fast update! what is the difference between a checkbox and option button? I thought they do the same thing? – user1204868 48 secs ago
感谢您的快速更新!复选框和选项按钮有什么区别?我以为他们做同样的事情?– user1204868 48 秒前
Excel VBA Option Buttons(Also known as radio buttons) are the same as Check Boxesexcept that Option Buttonsare dependent on each other while Check Boxesare not. When you check one Option Buttonthe other Option Buttonwill automatically get unchecked.
Excel VBA选项按钮(也称为单选按钮)与复选框相同,只是选项按钮相互依赖,而复选框则不是。当您选中一个选项按钮时,另一个选项按钮将自动取消选中。
See the snapshot below on how they look :)
请参阅下面的快照,了解它们的外观:)
Would recommend seeing the Excel's inbuilt VBA help for more details ;)
建议查看 Excel 的内置 VBA 帮助以获取更多详细信息;)
SNAPSHOT:
快照:
HTH
HTH
Sid
锡德
回答by hossein1976
I also had this problem because I didn't want to use option buttons on my form, so I have written some code for this case and it works! But there maybe a more appropriate way of writing code for this matter. Do as the following list:
我也遇到了这个问题,因为我不想在表单上使用选项按钮,所以我为这种情况编写了一些代码并且它有效!但是也许有一种更合适的方式来为这个问题编写代码。请按照以下列表进行操作:
(For example you have four checkboxes in your form, there is no difference in the number of them.)
(例如,您的表单中有四个复选框,它们的数量没有区别。)
1- Name your userform (which the checkboxes are on) with an appropriate name in it's properties window. The name property is the first in the properties window (in the alphabetic tab). This is for so that we can easier approach an call the userform when where writing the necessary code using the intelisense list.
1- 在其属性窗口中使用适当的名称命名您的用户表单(复选框所在的位置)。name 属性是属性窗口中的第一个(在字母选项卡中)。这是为了在使用智能感知列表编写必要的代码时,我们可以更轻松地调用用户表单。
For example: userform1
例如:userform1
2- Name your checkboxes with an appropriate name in their properties window. The name property is the first in the properties window (in the alphabetic tab). This is for so that we can easier approach an call the checkboxes when where writing the necessary code using the intelisense list.
2- 在其属性窗口中用适当的名称命名您的复选框。name 属性是属性窗口中的第一个(在字母选项卡中)。这是为了在使用智能感知列表编写必要的代码时,我们可以更轻松地调用复选框。
For example: chkbox1, chkbox2, chkbox3 and chkbox4
例如:chkbox1、chkbox2、chkbox3 和 chkbox4
3- Click on one of the checkboxes in the userform (in the vb editor) and you see that the click event has been generated. Click the right hand side drop down list and choose the "after updating" event. Erase the click event sub routine of that checkbox in order to tidy up the vb editor.
3- 单击用户窗体(在 vb 编辑器中)中的复选框之一,您会看到单击事件已生成。单击右侧的下拉列表并选择“更新后”事件。删除该复选框的单击事件子例程以整理 vb 编辑器。
4- In between this subroutine, for example:
4- 在此子程序之间,例如:
Private Sub Chkbox1_AfterUpdate()
End Sub
We will write such code:
我们将编写这样的代码:
Private Sub Chkbox1_AfterUpdate()
userform1.Chkbox2.Value = False
userform1.Chkbox3.Value = False
userform1.Chkbox4.Value = False
End Sub
Note 1: As you can see in this code, when the user clicks chkbox1, an afterupdate happens in which the value of all the chkboxes (except chkbox1) gets called and their values get turned into false.
注意 1:正如您在此代码中看到的,当用户单击 chkbox1 时,会发生 afterupdate,其中所有 chkbox(除了 chkbox1)的值都被调用,并且它们的值变为 false。
Note 2: As you can see, we have to write such for the other checkboxes' afterupdate events, but be careful to write the code only for the other checkboxes, for example if your writing code for chkbox1, then chxbox1 shouldn't be written it's code below it, this rule is for all the other checkboxes respectedly. If you don't write the code based on this rule, your code wont work then.
注意2:如你所见,我们必须为其他复选框的afterupdate事件编写这样的代码,但要注意只为其他复选框编写代码,例如如果你为chkbox1编写代码,则不应编写chxbox1它是下面的代码,此规则适用于所有其他复选框。如果您不根据此规则编写代码,那么您的代码将无法运行。
5- Write the proper code for the other 3 or etc checkboxes' afterevent respectedly. and the code is ready to work.
5- 为其他 3 个或其他复选框的事后事件编写正确的代码。并且代码已准备就绪。
If you want to see an overall view of the necessary code (just in case), for 4 checkboxes on 1 userform, it should look like this:
如果您想查看必要代码的整体视图(以防万一),对于 1 个用户表单上的 4 个复选框,它应该如下所示:
Private Sub Chkbox1_AfterUpdate()
userform1.Chkbox2.Value = False
userform1.Chkbox3.Value = False
userform1.Chkbox4.Value = False
End Sub
Private Sub Chkbox2_AfterUpdate()
userform1.Chkbox1.Value = False
userform1.Chkbox3.Value = False
userform1.Chkbox4.Value = False
End Sub
Private Sub Chkbox3_AfterUpdate()
userform1.Chkbox1.Value = False
userform1.Chkbox2.Value = False
userform1.Chkbox4.Value = False
End Sub
Private Sub Chkbox4_AfterUpdate()
userform1.Chkbox1.Value = False
userform1.Chkbox2.Value = False
userform1.Chkbox3.Value = False
End Sub
Note 3: The names you use for the userform and the checkboxes are up to you and there is no restriction. You can use Hungarian notations for having more proper names.
注意 3:您用于用户表单和复选框的名称由您决定,没有限制。您可以使用匈牙利符号来获得更多专有名称。
回答by JCarlos
Private Sub CheckBox1_Click()
If CheckBox2.Enabled = True Then
CheckBox2.Enabled = False
Else
CheckBox2.Enabled = True
End If
End Sub