通过CommandButton取消选中整个工作簿中的所有复选框

时间:2013-05-29 15:36:06

标签: excel excel-vba checkbox activexobject vba

我想要一个代码,为工作簿中的所有工作表取消选中名为“CheckBox1”的所有复选框。遗憾的是,我当前的代码不起作用,我不知道为什么 - 它只适用于活动表。

Private Sub CommandButton1_Click()

    Dim Sheet As Worksheet
    For Each Sheet In ThisWorkbook.Worksheets

        Select Case CheckBox1.Value
        Case True: CheckBox1.Value = False
        End Select  
    Next
End Sub

2 个答案:

答案 0 :(得分:5)

此代码遍历所有工作表(除了名为Sheet100OtherSheet 的工作表)和 取消选中 所有工作表 ActiveX 名为 CheckBox1

的复选框
Sub uncheck_boxes()

    Dim ws As Worksheet
    Dim xbox As OLEObject
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Sheet100" And ws.Name <> "OtherSheet" Then
            For Each xbox In ws.OLEObjects
                ws.OLEObjects("CheckBox1").Object.Value = False
            Next
        End If
    Next
End Sub

要取消选中所有工作表中的所有 ActiveX 复选框,而忽略所使用的名称

Sub uncheck_all_ActiveX_checkboxes()

    Dim ws As Worksheet
    Dim xbox As OLEObject
    For Each ws In ThisWorkbook.Worksheets
        For Each xbox In ws.OLEObjects
            ws.OLEObjects(xbox.Name).Object.Value = False
        Next
    Next
End Sub

要取消选中电子表格中的所有 Form Control checkboxes

Sub uncheck_forms_checkboxes()

    Dim ws As Worksheet
    Dim xshape As Shape
    For Each ws In ThisWorkbook.Worksheets
        For Each xshape In ws.Shapes
            If xshape.Type = msoFormControl Then
                xshape.ControlFormat.Value = False
            End If
        Next
    Next
End Sub

答案 1 :(得分:2)

[编辑以下评论]

试试这个:

Sub test()
Dim ws As Excel.Worksheet
Dim s As Object

For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Definitions" And ws.Name <> "fx" Then
        Set s = Nothing
        On Error Resume Next
        Set s = ws.OLEObjects("CheckBox1")
        On Error GoTo 0
        If Not s Is Nothing Then
            s.Object.Value = False
        End If
    End If
Next ws

End Sub

这是一个全局函数(它不属于特定工作表),但如果需要,可以将其放在CommandButton1_Click()内。

如果工作表(定义和fx除外)始终包含CheckBox1,则可能不需要阻止错误。或者删除if语句。