使用 VBA 宏取消选择 Excel 工作簿中的所有复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18927684/
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
Unselect All CheckBoxes From Excel Workbook with VBA Macro
提问by user2533460
I have a workbook with over 100 checkboxes.
我有一个包含 100 多个复选框的工作簿。
They are form control checkboxes
它们是表单控件复选框
I would like to un-select them all at once
我想一次取消选择它们
that is set them to false.
将它们设置为 false。
Sub clearcheck()
ActiveSheet.CheckBoxes.Value = False
End Sub
This works for the active sheet. I would like this code to be for the whole workbook
这适用于活动工作表。我希望此代码适用于整个工作簿
I have tried looking for the code and messing about with clearing the checkboxes but am none the wiser.
我曾尝试寻找代码并清除复选框,但我并不聪明。
I would really appreciate if some one could guide me
如果有人可以指导我,我将不胜感激
thank you
谢谢你
回答by Gary's Student
If you have OLEObject-style (ActiveX) checkboxes, then:
如果您有 OLEObject 样式 ( ActiveX) 复选框,则:
Sub terranian()
Dim o As Object
For Each o In ActiveSheet.OLEObjects
If InStr(1, o.Name, "CheckBox") > 0 Then
o.Object.Value = False
End If
Next
End Sub
EDIT1:
编辑1:
If they are forms checkboxes, then the following will work:
如果它们是表单复选框,那么以下将起作用:
Sub clearcheck()
Dim sh As Worksheet
For Each sh In Sheets
On Error Resume Next
sh.CheckBoxes.Value = False
On Error GoTo 0
Next sh
End Sub
回答by rjkunde
The selected answer is perfect, but I needed the ability to clear the active sheet OR all check-boxes via buttons for form checkboxes. Putting both sub routines inside of one "Module" worked for me. Then I created a button for each action, and mapped to the individual sub-routines.
选定的答案是完美的,但我需要能够通过表单复选框按钮清除活动工作表或所有复选框。将两个子例程放在一个“模块”中对我有用。然后我为每个动作创建了一个按钮,并映射到各个子例程。
'Uncheck all checkboxes in entire excel workbook
Sub uncheck_all()
Dim sh As Worksheet
For Each sh In Sheets
On Error Resume Next
sh.Checkboxes.Value = False
On Error GoTo 0
Next sh
End Sub
'Uncheck all checkboxes in active sheet
Sub uncheck_active_sheet()
ActiveSheet.Checkboxes.Value = False
End Sub