Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

excel - Uncheck all checkboxes across entire workbook via CommandButton

I would like to have a code that unchecks all checkboxes named "CheckBox1" for all sheets across the workbook. My current code unfortunately doesn't work, and I'm not sure why - it only works for the active sheet.

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This code iterates through all sheets (except sheets named Sheet100 and OtherSheet) and unchecks all your ActiveX checkboxes named 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

To uncheck all ActiveX checkboxes in all sheets disregarding the names used

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

To uncheck all Form Control checkboxes on a spreadsheet use

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...