Jul 07 2022 03:08 AM
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(xAddress).Hidden = True
Else
Application.ActiveSheet.Columns(xAddress).Hidden = False
End If
End Sub
But instead of F:G I want to select the columns based on a part of a name of a header in my table.
For example I have a table with these header names;
person1 - M1
person2 - M1
person3 - M2
I want to hide/unhide columns based upon the - M1 part of the headername.
Anyone knows a solution?
Jul 07 2022 04:31 AM
For example
Private Sub ToggleButton1_Click()
Const HeaderRow = 1
Dim rng As Range
Set rng = Rows(HeaderRow).Find(What:="* - M1", LookAt:=xlWhole)
If rng Is Nothing Then
MsgBox "M1 not found!", vbExclamation
Exit Sub
End If
rng.EntireColumn.Hidden = ToggleButton1.Value
End Sub
Jul 08 2022 01:01 AM
Jul 08 2022 02:57 AM
The code that I posted looks only for the first instance. Here is a version that should handle all instances:
Private Sub ToggleButton1_Click()
Const HeaderRow = 1
Dim rng As Range
Dim adr As String
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 Sub
Jul 11 2022 01:21 AM
Jul 11 2022 02:22 AM
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 Sub
Jul 11 2022 04:25 AM
Jul 11 2022 04:35 AM
The old code toggles the visibility of columns with M1 on and off.
What exactly should the new code do? Hide M2 - M5 and show M1? Or toggle - if so, what?
Jul 11 2022 04:46 AM
Perhaps this?
Private Sub ShowOnlyM1()
Const HeaderRow = 1
Dim rng As Range
Dim adr As String
Cells.EntireColumn.Hidden = True
Set rng = Rows(HeaderRow).Find(What:="* - M1", LookAt:=xlWhole)
If Not rng Is Nothing Then
adr = rng.Address
Do
rng.EntireColumn.Hidden = False
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
Range("A1").Select
End Sub
Jul 11 2022 04:56 AM
Jul 11 2022 05:31 AM
The code works for me. Could you attach a sample workbook (without sensitive data), or if that is not possible, make it available through OneDrive, Google Drive, Dropbox or similar? Thanks in advance.
Jul 11 2022 05:34 AM
Jul 11 2022 06:22 AM
Thanks, that helped. See the attached version. I added 5 command buttons.
Jul 11 2022 07:35 AM
Jul 11 2022 08:03 AM - edited Jul 11 2022 11:24 AM
The line
Range("C1").Resize(1, LastColumn - 5).EntireColumn.Hidden = True
hides all columns in the used range except for the first two and the last three. In the sample workbook, the data extend from column A to column R, so columns C to O get hidden, while columns A, B, P, Q and R will always remain visible.
Jul 11 2022 09:02 AM - edited Jul 11 2022 09:03 AM
Thank you so much!