Built in tool or VBA to hide all unused columns with a catch

xdriver

Board Regular
Joined
Mar 21, 2014
Messages
73
Office Version
  1. 365
Platform
  1. MacOS
I have a spreadsheet that is exported from one of my databases that has nearly 900 columns with headers, however not all columns have data in them other than the header. Is there a built in toggle to hide the columns without data in them? I would obviously want to hide the header row if there is nothing else in the column, as well as a way to unhide all columns at once if possible. Thank you.

Excel for Mac 365 16.8.2
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
try this

VBA Code:
Sub hide_blanks()

' this will hide the column if its blank or if there is only 1 cell with data (The header)
Application.ScreenUpdating = False
Application.EnableEvents = False

On Error Resume Next

For c = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
   If Columns(c).Cells.SpecialCells(xlCellTypeConstants).Count = 1 Then Columns(c).Hidden = True
Next c

On Error GoTo 0
Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub

VBA Code:
Sub Show_All_columns()
Columns.EntireColumn.Hidden = False

End Sub
 
Upvote 1
Solution
try this

VBA Code:
Sub hide_blanks()

' this will hide the column if its blank or if there is only 1 cell with data (The header)
Application.ScreenUpdating = False
Application.EnableEvents = False

On Error Resume Next

For c = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
   If Columns(c).Cells.SpecialCells(xlCellTypeConstants).Count = 1 Then Columns(c).Hidden = True
Next c

On Error GoTo 0
Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub

VBA Code:
Sub Show_All_columns()
Columns.EntireColumn.Hidden = False

End Sub

That appears to have worked perfectly. Thank you so much. I figured it had to be something with "count." I should really start trying to learn some VBA for myself. Again, thank you very much.
 
Upvote 0
Actually, how would I modify that for a range of count values? If I decided that I wanted to hide a column with 1-5 total values.
 
Upvote 0
If Columns(c).Cells.SpecialCells(xlCellTypeConstants).Count =<5 Then Columns(c).Hidden = True
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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