macro to hide columns with zero values?

K

K Dawson

I'm relatively new to VBA and need to create a macro to hide columns with
zero values. The workbook has 36 sheets, each sheet has columns B:CR and 202
rows. Row 202 is the sum of the respective column.
I'd like to run the macro in a separate workbook so I can use it for other
similar workbook situations.
Any suggestions would be greatly appreciated!
 
J

JLatham

I believe the code below will work for you. It will examine all used cells
on the row on a selected sheet where a cell is selected and hide all columns
with an empty cell or a zero value cell on that row. In your example, you'd
select any cell in row 202 of the sheet and then run the macro.

Sub HideZeroTotalColumns()
'this works with the active sheet
'in whatever book, and all you need
'to do is select the book, sheet and
'a cell in the "total" row to then
'hide all columns with a zero total

Dim anyWS As Worksheet
Dim anyTotalRow As Range
Dim anyCell As Range
Set anyWS = ActiveSheet
Set anyTotalRow = anyWS.Range(Cells(ActiveCell.Row, 1).Address, _
Cells(ActiveCell.Row, _
anyWS.Cells(ActiveCell.Row, Columns.Count). _
End(xlToLeft).Column))
'improve performance speed
Application.ScreenUpdating = False
'unhide in case was hidden and is now
'non-zero
anyTotalRow.Columns.EntireColumn.Hidden = False
For Each anyCell In anyTotalRow
If IsEmpty(anyCell) Or anyCell = 0 Then
anyCell.EntireColumn.Hidden = True
End If
Next
Set anyTotalRow = Nothing
Set anyWS = Nothing
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