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
 
How about
VBA Code:
Sub knighttrader()
   With Range("A3:LZ3").SpecialCells(xlConstants).EntireColumn
      .Hidden = Not .Hidden
   End With
End Sub
 
Upvote 0
Solution

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
How about
VBA Code:
Sub knighttrader()
   With Range("A3:LZ3").SpecialCells(xlConstants).EntireColumn
      .Hidden = Not .Hidden
   End With
End Sub
Perfect.

Many thanks again Fluff. Very grateful for help and lesson.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,999
Messages
6,122,645
Members
449,093
Latest member
Ahmad123098

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