Forum Discussion

MrPet88's avatar
MrPet88
Copper Contributor
Apr 25, 2024

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").En...
  • HansVogelaar's avatar
    Apr 25, 2024

    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

Resources