Hide Unhide Columns not in a range

knighttrader

New Member
Joined
Apr 3, 2010
Messages
21
Office Version
  1. 2021
Platform
  1. MacOS
Hi

Using Excel on a Mac

I want to hide/unhide multiple columns that are not in a range using one button. e.g. Columns, A, E, S, T, Y

I want to hide/Unhide 45 columns in total. The following code works, but can anyone show me how to write the code better?

Many thanks

VBA Code:
Sub HideUnhideLayColumns()

Columns("A").EntireColumn.Select
If Selection.EntireColumn.Hidden = False Then
Selection.EntireColumn.Hidden = True
Else
Selection.EntireColumn.Hidden = False
End If

Columns("E").EntireColumn.Select
If Selection.EntireColumn.Hidden = False Then
Selection.EntireColumn.Hidden = True
Else
Selection.EntireColumn.Hidden = False
End If

End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try:

Columns("A,E,S,T,Y").EntireColumn.Hidden = True
 
Upvote 0
How about
VBA Code:
Sub knighttrader()
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Array("A", "E", "S", "T", "Y")
   For i = LBound(Ary) To UBound(Ary)
      Columns(Ary(i)).Hidden = Not Columns(Ary(i)).Hidden
   Next i
End Sub
 
Upvote 0
Glad to help & thanks for the feedback.

Although your solution works I realise that it would be better if I could select the columns to be hidden based on a cell value rather specify the columns in the code.
The columns that I want to hide/unhide will change over time and I don't want to have to change the code every time.

This is the plan:
Range = A : LZ
I will type "HIDE' in ROW 3 for each column that needs to be hidden.
When I hit the button all columns within the range with HIDE in Row 3 should hide or unhide.

Can you help with that?
 
Upvote 0
Will there be anything else in row 3 other than the word Hide?
 
Upvote 0
Ok, how about
VBA Code:
Sub knighttrader()
   Range("A3:LZ3").SpecialCells(xlConstants).EntireColumn.Hidden = True
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub knighttrader()
   Range("A3:LZ3").SpecialCells(xlConstants).EntireColumn.Hidden = True
End Sub
Thanks. That works as far as hiding columns, however, it doesn't unhide. Is it possible to unhide hidden columns using the same button as before?
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

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