Forum Discussion
Activate/deactivate checkboxes using a macro, when those checkboxes have also macros assigned
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
Then I create a module and configure it so it activates and deactivates all the checkboxes
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
Thanks in advance
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
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
- MrPet88Copper Contributor