SOLVED

Activate/deactivate checkboxes using a macro, when those checkboxes have also macros assigned

Copper Contributor

I have several checkboxes in my table, and each check box as a macro assigned to hide or unhide a column, depending on if it is checked or not. For example:

 

Sub HideUnhideC()
    Range("C:C").EntireColumn.Hidden = _
        (ActiveSheet.Shapes("Check Box 1").ControlFormat.Value <> 1)
End Sub
Sub HideUnhideD()
    Range("D:D").EntireColumn.Hidden = _
        (ActiveSheet.Shapes("Check Box 2").ControlFormat.Value <> 1)
End Sub

 

I would like to create a button, that activate and deactivates all the checkboxes at once. For this I have created a Command button with ActiveX Controls

MrPet88_0-1714051578975.png

Then I create a module and configure it so it activates and deactivates all the checkboxes

MrPet88_1-1714051658342.png

 

 

Private Sub CommandButton1_Click()
For Each cb In Sheet1.CheckBoxes
cb.Value = xlOn
Next

End Sub

Private Sub CommandButton2_Click()
For Each cb In Sheet1.CheckBoxes
cb.Value = xlOff
Next

End Sub

 

The buttons seem to work well and activate or deactivate the checkboxes, but it doesn't trigger the hiding/unhiding function.

What am I doing wrong? I am pretty new with macros

MrPet88_2-1714051828795.png

Thanks in advance

2 Replies
best response confirmed by MrPet88 (Copper Contributor)
Solution

@MrPet88 

Use ActiveX check boxes instead of Form Control check boxes.

All code then goes into the worksheet module:

Private Sub CheckBox1_Click()
    Range("C:C").EntireColumn.Hidden = Not Me.CheckBox1
End Sub

Private Sub CheckBox2_Click()
    Range("D:D").EntireColumn.Hidden = Not Me.CheckBox2
End Sub

Private Sub CommandButton1_Click()
    Dim ctl As OLEObject
    For Each ctl In Me.OLEObjects
        If TypeName(ctl.Object) = "CheckBox" Then
            ctl.Object.Value = True
        End If
    Next ctl
End Sub

Private Sub CommandButton2_Click()
    Dim ctl As OLEObject
    For Each ctl In Me.OLEObjects
        If TypeName(ctl.Object) = "CheckBox" Then
            ctl.Object.Value = False
        End If
    Next ctl
End Sub

@Hans Vogelaar 

That was exactly what I was looking for.

Thank you very much

1 best response

Accepted Solutions
best response confirmed by MrPet88 (Copper Contributor)
Solution

@MrPet88 

Use ActiveX check boxes instead of Form Control check boxes.

All code then goes into the worksheet module:

Private Sub CheckBox1_Click()
    Range("C:C").EntireColumn.Hidden = Not Me.CheckBox1
End Sub

Private Sub CheckBox2_Click()
    Range("D:D").EntireColumn.Hidden = Not Me.CheckBox2
End Sub

Private Sub CommandButton1_Click()
    Dim ctl As OLEObject
    For Each ctl In Me.OLEObjects
        If TypeName(ctl.Object) = "CheckBox" Then
            ctl.Object.Value = True
        End If
    Next ctl
End Sub

Private Sub CommandButton2_Click()
    Dim ctl As OLEObject
    For Each ctl In Me.OLEObjects
        If TypeName(ctl.Object) = "CheckBox" Then
            ctl.Object.Value = False
        End If
    Next ctl
End Sub

View solution in original post