Macro to hide colums with value of zero or blank

J

Jodie

I need to hide columns in all worksheets in the workbook that have only zeros
or only blank cells after the first two rows. For example, Column A is
headed Names, Col B - Fee 1 and Col C - Fee2 and Col D - Comments. If all of
the fees under Fee2 are 0, then I want to hide the entire column. If there
are no comments, then I want to hide that column as well. The column
headings are in row 2. Can anyone help with this?
 
D

Don Guillett

If desired, send your file to my address below. I will only look if:
1. You send a copy of this message on an inserted sheet
2. You give me the newsgroup and the subject line
3. You send a clear explanation of what you want
4. You send before/after examples and expected results.
 
D

Don Guillett

For the wb presented, this should do it

Option Explicit
Sub HideBlankColumnsSAS()
Dim w As Long
Dim lc As Long
Dim i As Long
Dim mc As Long

For w = 1 To Worksheets.Count
With Sheets(w)
..Columns.Hidden = False
lc = .Cells(2, Columns.Count).End(xlToLeft).Column

For i = lc To 7 Step -1
mc = Application.CountA(.Columns(i))
If mc < 2 Or mc > 1 And Application.Sum(.Columns(i)) = 0 Then
.Columns(i).Hidden = True
End If
Next i

End With
Next w
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top