3

我想要一个代码来取消选中工作簿中所有工作表的所有名为“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
4

2 回答 2

5

此代码遍历所有工作表(名为Sheet100andOtherSheet的工作表除外)并取消选中所有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
于 2013-05-29T16:05:03.520 回答
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 语句。

于 2013-05-29T15:42:05.423 回答