Hide columns with no count

xdriver

Board Regular
Joined
Mar 21, 2014
Messages
73
Office Version
  1. 365
Platform
  1. MacOS
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.
 
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
 
Upvote 0
Solution

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
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.
 
Upvote 0

Forum statistics

Threads
1,213,491
Messages
6,113,963
Members
448,536
Latest member
CantExcel123

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