Mar 06 2023 11:07 AM
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.
Mar 06 2023 11:17 AM
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.
Mar 06 2023 11:46 AM
Mar 06 2023 11:57 AM
That's easy:
Sub HideUnhide()
Range("K1:L1").EntireColumn.Hidden = _
(ActiveSheet.Shapes("Check Box 1").ControlFormat.Value <> 1)
End Sub