Home » excel » excel – Check box to select and deselect all other check boxes in spreadsheet

excel – Check box to select and deselect all other check boxes in spreadsheet

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a spreadsheet that pulls line items in with check boxes for each line item. I would like to put a check box at the top that will select and deselect all the other check boxes in the spreadsheet when it is selected/deselected. Following is the code I have so far which will select all the check boxes if my “Check Box 1” is selected but it won’t deselect them if it is deselected. What do I need to add to make the boxes also deselect if “Check Box 1” is deselected. Thanks for any help.

Sub SelectAllCheckBox()
Dim CB As CheckBox
If ActiveSheet.CheckBoxes("Check Box 1").Value Then
    For Each CB In ActiveSheet.CheckBoxes
        If CB.Name <> ActiveSheet.CheckBoxes("Check Box 1").Name Then
            CB.Value = True
        End If
    Next CB
End If
End Sub

I am also running into another problem. I have a macro to clear the sheet so different macros can run. This macro has code in it that deletes all check boxes. How would I word the code to make it not delete “Check Box 1”. This is the code I have.

Sheets("Quote Sheet").Select
Range("D3:D7").Select
Selection.ClearContents
Rows("11:1000").Select
Selection.Delete Shift:=xlUp
ActiveSheet.CheckBoxes.Delete
Selection.FormatConditions.Delete

I tried the following but it didn’t work.

Sheets("Quote Sheet").Select
Range("D3:D7").Select
Selection.ClearContents
Rows("11:1000").Select
Selection.Delete Shift:=xlUp
If CB.Name <> ActiveSheet.CheckBoxes("Check Box 1").Name Then    
ActiveSheet.CheckBoxes.Delete
Selection.FormatConditions.Delete
How to&Answers:
Sub SelectAllCheckBox()
    Dim CB As CheckBox

    For Each CB In ActiveSheet.CheckBoxes
        If CB.Name <> ActiveSheet.CheckBoxes("Check Box 1").Name Then
            CB.Value = ActiveSheet.CheckBoxes("Check Box 1").Value
        End If
    Next CB

End Sub

Second part:

Dim CB as CheckBox, n as long, x as long

n = ActiveSheet.CheckBoxes.Count

For x = n to 1 Step -1        
    Set CB = ActiveSheet.CheckBoxes(x)
    If CB.Name <> "Check Box 1" Then CB.Delete

Next x