Forum Discussion

tmadaras485's avatar
tmadaras485
Copper Contributor
Mar 06, 2023

Hide/unhide Columns using checkbox

I want to hide/unhide a couple of columns with a checkbox. 

Say columns K and L are the two columns I want to hide/unhide  - how would I go about doing that.

3 Replies

  • tmadaras485 

    Insert a check box from the Form Controls section of the Insert Controls drop down on the Developer tab of the ribbon. In the following, I'll assume that its name is Check Box 1.

    Right-click the check box and select Assign Macro... from the context menu.

    Enter the name HideUnhide and click New.

    Make the code look like this:

    Sub HideUnhide()
        Range("K1:L1").EntireColumn.Hidden = _
            (ActiveSheet.Shapes("Check Box 1").ControlFormat.Value = 1)
    End Sub

    Switch back to Excel.

    Save the workbook as a macro-enabled workbook (*.xlsm).

    Make sure that you allow macros when you open it.

    • tmadaras485's avatar
      tmadaras485
      Copper Contributor
      hide the columns when it is checked I want to unhide when it is checked.
      • HansVogelaar's avatar
        HansVogelaar
        MVP

        tmadaras485 

        That's easy:

        Sub HideUnhide()
            Range("K1:L1").EntireColumn.Hidden = _
                (ActiveSheet.Shapes("Check Box 1").ControlFormat.Value <> 1)
        End Sub

Resources