show hide multiple columns

mattstan2012

New Member
Joined
May 23, 2024
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi

I have managed to find the following which allows me to toggle between showing and hiding a specific column using a toggle button a and VGA:
Private Sub ViewHideLunches_Click()
Dim MyC As String
MyC = "C:D"
If ViewHideLunches.Value Then

Application.ActiveSheet.Columns(MyC).Hidden = True
Else

Application.ActiveSheet.Columns(MyC).Hidden = False

End If

End Sub

It works ace for hiding columns C & D, but I need to also hide C&D, H&I, M&N etc etc but I can't get it to do this.

I thought having MyC = "C:D","H:I","M:N" etc would work but, of course, it doesn't.

Any suggestions?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
You can use Union to add all the columns to your range:

VBA Code:
Dim MyC As Range
Set MyC = Union(Range("C:D"), Range("H:I"))
MyC.Columns.Hidden = True
 
Upvote 0
Hi Dreid 1011 - thank you for looking - Unless I've entered it incorrectly (which is likely) it seems to hide them, but not, unhide them:


Private Sub ViewHideLunches_Click()
Dim MyC As Range

Set MyC = Union(Range("C:D"), Range("H:I"))

MyC.Columns.Hidden = True

If ViewHideLunches.Value Then

Application.ActiveSheet.Columns(MyC).Hidden = True
Else

Application.ActiveSheet.Columns(MyC).Hidden = False


End If

End Sub
 
Upvote 0
Hi
try this update to your code & see if helps

VBA Code:
Private Sub ViewHideLunches_Click()
    Dim MyC As String
    
    MyC = "C:D,H:I,M:N"
    
    ActiveSheet.Range(MyC).EntireColumn.Hidden = ViewHideLunches.Value

End Sub

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top