Hide/Unhide Multiple columns

johannes2008

New Member
Joined
Aug 20, 2010
Messages
42
Office Version
  1. 365
Platform
  1. Windows
Hello

I am trying to create a button that hides/unhides multiple columns that are not adjacent (ie col A, X, AB,BX). My plan is to assign all the columns to a variable and then use an if/else statement for hide/unhide but I don't know how to assign multiple ranges to a variable. Is my plan workable, if not what do you suggest?

Thanks
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Here's how to hide multiple, non-adjacent columns:
VBA Code:
Dim rng As Range
Set rng = Range("A:B,F:G,X:X,Z:AB")
rng.EntireColumn.Hidden = True
or simply this, without using a variable:
VBA Code:
Range("A:B,F:G,X:X,Z:AB").EntireColumn.Hidden = True
 
Upvote 0
To hide:
VBA Code:
Range("A:A, X:X, AB:AB ,BX:BX").EntireColumn.Hidden = True

Set to False to unhide.
 
Upvote 0
Here's how to hide multiple, non-adjacent columns:
VBA Code:
Dim rng As Range
Set rng = Range("A:B,F:G,X:X,Z:AB")
rng.EntireColumn.Hidden = True
or simply this, without using a variable:
VBA Code:
Range("A:B,F:G,X:X,Z:AB").EntireColumn.Hidden = True

This is the code i am trying to use

VBA Code:
Private Sub CommandButton1_Click()

Dim rng As Range
Set rng = Range("AC:AD,AI,AK:AL,AQ,AS:AT")

If rng.EntireColumn.Hidden = False Then
    rng.EntireColumn.Hidden = True
    Else
        rng.EntireColumn.Hidden = False
End If
End Sub

but I get an error.
 
Upvote 0
First, you reference AI and AQ alone, they should be AI:AI and AQ:AQ. Second, in your example you don't need to test if the columns are hidden if you're just going to reverse their state, you can just reverse their state using "Not", e.g.:
VBA Code:
Range("AC:AD,AI:AI,AK:AL,AQ:AQ,AS:AT").EntireColumn.Hidden = Not Range("AC:AD,AI:AI,AK:AL,AQ:AQ,AS:AT").EntireColumn.Hidden
Running this macro multiple times will simply hide/unhide the set of columns each time.
 
Upvote 0
Solution
VBA Code:
       Private Sub CommandButton1_Click()

Dim rng As Range
Set rng = Range("AC:AD,AI:AI,AK:AL,AQ:AQ,AS:AT")

If rng.EntireColumn.Hidden = False Then
rng.EntireColumn.Hidden = True
Else
rng.EntireColumn.Hidden = False
End If
End Sub
 
Upvote 0
First, you reference AI and AQ alone, they should be AI:AI and AQ:AQ. Second, in your example you don't need to test if the columns are hidden if you're just going to reverse their state, you can just reverse their state using "Not", e.g.:
VBA Code:
Range("AC:AD,AI:AI,AK:AL,AQ:AQ,AS:AT").EntireColumn.Hidden = Not Range("AC:AD,AI:AI,AK:AL,AQ:AQ,AS:AT").EntireColumn.Hidden
Running this macro multiple times will simply hide/unhide the set of columns each time.
Oh right. Fixed that and used your "Not" code and it worked perfectly. Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,472
Members
449,087
Latest member
RExcelSearch

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