PDA

View Full Version : Hide/unhide columns



keith barnes
01-13-2023, 06:49 PM
Hi Guys

I have some code that works ok to hide and unhide columns.




Sub hideit()
Range("C:N").EntireColumn.Hidden = True
End Sub

Sub unhideit()
Range("C:N").EntireColumn.Hidden = False
End Sub



Can someone please show me how to toggle both macro using 1 command button. ie, click once and the column hides-click again and the column appears.

If this is not possible then maybe option buttons of something else, but i would really like a command button.

PS these command buttons are active x in a userform

June7
01-14-2023, 12:13 AM
Perhaps with a toggle button. In Access VBA it would be like:


Sub HideUnhide_Click()
With Me.HideUnhide
Me.Start.Enabled = Not .Value
.Caption = IIf(.Value, "Unhide", "Hide")
End With
End Sub

Apparently, in Excel there is an ActiveX toggle button but I am not sure how to reference in VBA.

Instead of my Enabled line you would have

Range("C:N").EntireColumn.Hidden = Not .Value

Artik
01-15-2023, 08:50 PM
Assuming that the full range of C:N columns will be hidden or uncovered, you can test one of the columns, e.g.

Columns("C:N").Hidden = Not Columns("C").Hidden
Artik

Aussiebear
01-15-2023, 09:33 PM
or another method to consider. Assign this code to your button


Private Sub cmdToggle_Click()
With Range("C:N").EntireColumn
.Hidden = Not .Hidden
End With
End Sub