show hide multiple columns

mattstan2012

New Member
Joined
May 23, 2024
Messages
12
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

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
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,217,383
Messages
6,136,254
Members
450,001
Latest member
KWeekley08

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