vba 检查是否选中了基于工作表的复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11741836/
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
Checking if a worksheet-based checkbox is checked
提问by Paolo Bernasconi
I'm trying to use an IF-clause to determine whether my checkbox, named "Check Box 1", is checked.
我正在尝试使用 IF 子句来确定是否选中了名为“Check Box 1”的复选框。
My current code:
我目前的代码:
Sub Button167_Click()
If ActiveSheet.Shapes("Check Box 1") = True Then
Range("Y12").Value = 1
Else
Range("Y12").Value = 0
End If
End Sub
This doesn't work. The debugger is telling me there is a problem with the
这不起作用。调试器告诉我有问题
ActiveSheet.Shapes("Check Box 1")
However, I know this code works (even though it serves a different purpose):
但是,我知道此代码有效(即使它用于不同的目的):
ActiveSheet.Shapes("Check Box 1").Select
With Selection
.Value = xlOn
My checkboxes (there are 200 on my page), are located in sheet1, by the name of "Demande". Each Checkbox is has the same formatted name of "Check Box ...".
我的复选框(我的页面上有 200 个)位于 sheet1 中,名称为“Demande”。每个 Checkbox 都具有相同的格式名称“Check Box ...”。
回答by Motes
Sub Button167_Click()
If ThisWorkbook.Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value = 1 Then
Range("Y12").Value = 1
Else
Range("Y12").Value = 0
End If
End Sub
1 is checked, -4146 is unchecked, 2 is mixed (grey box)
1 选中,-4146 未选中,2 混合(灰色框)
回答by Siddharth Rout
Is this what you are trying?
这是你正在尝试的吗?
Sub Sample()
Dim cb As Shape
Set cb = ActiveSheet.Shapes("Check Box 1")
If cb.OLEFormat.Object.Value = 1 Then
MsgBox "Checkbox is Checked"
Else
MsgBox "Checkbox is not Checked"
End If
End Sub
Replace Activesheet
with the relevant sheetname. Also replace Check Box 1
with the relevant checkbox name.
替换Activesheet
为相关的工作表名称。也替换Check Box 1
为相关的复选框名称。
回答by JimmyPena
Building on the previousanswers, you can leverage the fact that True
is -1 and False
is 0 and shorten your code like this:
基于先前的答案,您可以利用True
-1 和False
0的事实并像这样缩短代码:
Sub Button167_Click()
Range("Y12").Value = _
Abs(Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value > 0)
End Sub
If the checkbox is checked, .Value
= 1.
如果复选框被选中,.Value
= 1。
Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value > 0
returns True
.
Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value > 0
返回True
。
Applying the Abs
function converts True
to 1
.
应用该Abs
函数转换True
为1
.
If the checkbox is unchecked, .Value
= -4146.
如果未选中该复选框,则.Value
= -4146。
Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value > 0
returns False
.
Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value > 0
返回False
。
Applying the Abs
function converts False
to 0
.
应用该Abs
函数转换False
为0
.
回答by Allan F
It seems that in VBA macro code for an ActiveX checkbox control you use
似乎在您使用的 ActiveX 复选框控件的 VBA 宏代码中
If (ActiveSheet.OLEObjects("CheckBox1").Object.Value = True)
如果 (ActiveSheet.OLEObjects("CheckBox1").Object.Value = True)
and for a Form checkbox control you use
对于您使用的表单复选框控件
If (ActiveSheet.Shapes("CheckBox1").OLEFormat.Object.Value = 1)
如果 (ActiveSheet.Shapes("CheckBox1").OLEFormat.Object.Value = 1)
回答by PowerUser
Try: Controls("Check Box 1") = True
尝试: Controls("Check Box 1") = True