Forum Discussion
Celia9
Jul 07, 2022Brass Contributor
VBA to dynamic toggle columns hide/unhide
Hi, I have a VBA to toggle my columns hide/unhide Private Sub ToggleButton1_Click() Dim xAddress As String xAddress = "F:G" If ToggleButton1.Value Then Application.ActiveSheet.Columns(xAdd...
HansVogelaar
Jul 11, 2022MVP
Do you want the toggle button to toggle the visibility of all columns based on whether their header ends in "- M1"? If so, try this:
Private Sub ToggleButton1_Click()
Const HeaderRow = 1
Dim rng As Range
Dim adr As String
Cells.EntireColumn.Hidden = Not ToggleButton1.Value
Set rng = Rows(HeaderRow).Find(What:="* - M1", LookAt:=xlWhole)
If Not rng Is Nothing Then
adr = rng.Address
Do
rng.EntireColumn.Hidden = ToggleButton1.Value
Set rng = Rows(HeaderRow).Find(What:="* - M1", After:=rng, LookAt:=xlWhole)
If rng Is Nothing Then Exit Do
Loop Until rng.Address = adr
End If
End SubCelia9
Jul 11, 2022Brass Contributor
I have 5 groups M1, M2, M3, M4 and M5
I would like to hide all, except M1.
Is that what you wanted this code to do? I get runtime error 1004
Application-defined or object-defined error
I would like to hide all, except M1.
Is that what you wanted this code to do? I get runtime error 1004
Application-defined or object-defined error