Macro to Hide Columns with ZERO value

share_knowledge

Board Regular
Joined
Aug 20, 2004
Messages
120
Hi -

Could someone please show me a better way to hide all columns with zero value in my excel 2003 file? It would be great that i could have a macro embed in my workbook/sheet that automatically hide all these zero value columns and only show the ones with valid values. Currently, i have to mananually select all columns with zero value and hide them one by one. This takes so much time since i have more than 6-7k rows. Thanks!

--Tony
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Any column that has at least one zero value should be hidden?
 
Upvote 0
Actually i wanted to hide the only columns with ZERO value in ALL rows? Here is an example:

Column A with 100 rows where there is one row with a value greater one = I want this column to be visible.

Column B with 100 rows where there is ZERO value in ALL rows = I'd like to automatically hide this column with a macro or with any quick way.

Can i also have another macro that do the same job but for ROW hidden instead of COLUMN? For the task in have on hand, i just need to hide all columns with all zero values in the rows. Thanks again!
 
Upvote 0
Here is the code i copied from Aki-Amur. Thanks!

Sub Macro2()



For Each r In Range("B1:AD1")

If r.Value = 0 Then

r.EntireColumn.Hidden = True

Else

If r.Value = "" Then

r.EntireColumn.Hidden = True

Else

r.EntireColumn.Hidden = False

End If

End If

Next



End Sub
 
Upvote 0
Code:
Public Sub testSum()
  Dim rng As Excel.Range
  
  For Each rng In ActiveSheet.UsedRange.Columns
    If Application.WorksheetFunction.Sum(rng) = 0 Then
      rng.EntireColumn.Hidden = True
    End If
  Next rng
End Sub

Let's suppose you want to do this each time a worksheet change occurs. Further let's suppose that your worksheet changes as a result of certain macros, not just by manual entry. In your worksheet's code module, use this code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Application.ScreenUpdating = False
  Dim rng As Excel.Range
  
  For Each rng In ActiveSheet.UsedRange.Columns
    If Application.WorksheetFunction.Sum(rng) = 0 Then
      rng.EntireColumn.Hidden = True
    Else
      rng.EntireColumn.Hidden = False
    End If
  Next rng
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Code:
Dim oneColumn As Range
For Each oneColumn In ActiveSheet.UsedRange.Columns
    oneColumn.Hidden = (Application.CountIf(oneColumn, 0) = Application.Count(oneColumn))
Next oneColumn

You can use COUNTA depending on how you want text cells handled.
 
Upvote 0
Good point... counting zeroes is the better way, since summing may yield zero even when some cells are not.
 
Upvote 0
Mike

Using the same basic structure, any idea how Max/Min would go for speed?

<font face=Courier New><SPAN style="color:#00007F">Dim</SPAN> oneColumn <SPAN style="color:#00007F">As</SPAN> Range<br><br><SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> oneColumn <SPAN style="color:#00007F">In</SPAN> ActiveSheet.UsedRange.Columns<br>    oneColumn.Hidden = (WorksheetFunction.Max(oneColumn) = 0) _<br>                        * (WorksheetFunction.Min(oneColumn) = 0)<br><SPAN style="color:#00007F">Next</SPAN> oneColumn</FONT>
 
Upvote 0
How can I apply this macro on all worksheets


Code:
Dim oneColumn As Range
For Each oneColumn In ActiveSheet.UsedRange.Columns
    oneColumn.Hidden = (Application.CountIf(oneColumn, 0) = Application.Count(oneColumn))
Next oneColumn

You can use COUNTA depending on how you want text cells handled.
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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