Hide columns with no count

xdriver

New Member
Joined
Mar 21, 2014
Messages
43
I have a worksheet that I would like to hide all columns that have a zero count since some cells contain numerical data and some contain text. All columns have a header, so those should be excluded in the count. I read this link Macro to Hide Columns with ZERO value - Mr. Excel but I think it's not doing what I would like because changing the count to countA includes the header row, and every column has header text. Thank you.
 

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
6,184
Office Version
  1. 365
Platform
  1. Windows
I think I have it now.

VBA Code:
Option Explicit

Sub HideMT()
    Dim lr As Long, lc As Long
    Dim i As Long
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 1 To lc
        lr = Cells(Rows.Count, i).End(xlUp).Row
        If WorksheetFunction.CountA(Range(Cells(2, i), Cells(lr + 1, i))) = 0 Then
            Cells(1, i).EntireColumn.Hidden = True
        End If
        
    Next i
End Sub
 
Solution

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

xdriver

New Member
Joined
Mar 21, 2014
Messages
43
I think I have it now.

VBA Code:
Option Explicit

Sub HideMT()
    Dim lr As Long, lc As Long
    Dim i As Long
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 1 To lc
        lr = Cells(Rows.Count, i).End(xlUp).Row
        If WorksheetFunction.CountA(Range(Cells(2, i), Cells(lr + 1, i))) = 0 Then
            Cells(1, i).EntireColumn.Hidden = True
        End If
       
    Next i
End Sub
That appears to have done it! Thank you very much. Not sure how to convey how many columns it hid, but it sure was a lot. Being able to use that code moving forward is a huge time saver and a lot easier on the eyes. Thank you again.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,383
Messages
5,635,951
Members
416,889
Latest member
dhegs

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
Top