Auto Hide Rows If 0 BUT Display When It Changes-VBA

Xiangwu

New Member
Joined
Feb 17, 2014
Messages
25
Hi, All,

I have a table that 20 items can change according to different conditions (years or quantity), but each year may not have all 20 items (Quantity might be 0). My ultimate goal is to generate a summary automatically from this table, but I don't know how to do so. I am thinking to repeat all 20 items for each year, and when the value is 0, I will hide the rows. The following is the code I developed.

Code:
[COLOR=#00008B][FONT=Consolas]Sub[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] HideRows[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]()[/FONT][/COLOR]
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit;">     [COLOR=#00008B]Dim[/COLOR] cell [COLOR=#00008B]As[/COLOR] Range    
[COLOR=#00008B]    For[/COLOR] [COLOR=#00008B]Each[/COLOR] cell [COLOR=#00008B]In[/COLOR] Range([COLOR=#800000]"C:C"[/COLOR])        
[COLOR=#00008B]        If[/COLOR] [COLOR=#00008B]Not[/COLOR] isEmpty(cell) [COLOR=#00008B]Then[/COLOR]            
[COLOR=#00008B]            If[/COLOR] cell.Value = [COLOR=#800000]0[/COLOR] [COLOR=#00008B]Then[/COLOR]                 
                cell.EntireRow.Hidden = [COLOR=#800000]True[/COLOR]            
[COLOR=#00008B]            End[/COLOR] [COLOR=#00008B]If[/COLOR]        
[COLOR=#00008B]        End[/COLOR] [COLOR=#00008B]If[/COLOR]    
[COLOR=#00008B]      Next[/COLOR] 
</code>[COLOR=#00008B][FONT=Consolas]  End [/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]Sub
[/FONT][/COLOR]

This works fine to hide the 0 value rows, but when the row is no longer 0, it's hidden. How can I adjust when the value changes, it will unhide the rows?

Many thanks in advance,

Xiangwu
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try:
Code:
Sub HideRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim cell As Range
    ActiveSheet.UsedRange.Rows.EntireRow.Hidden = False
    For Each cell In Range("C2:C" & LastRow)
        If cell.Value = 0 Then
            cell.EntireRow.Hidden = True
        End If
    Next cell
    Application.ScreenUpdating = True
End Sub
If the rows are hidden, how do you change the values in column C from zero to something else?
 
Upvote 0
Try:
Code:
Sub HideRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim cell As Range
    ActiveSheet.UsedRange.Rows.EntireRow.Hidden = False
    For Each cell In Range("C2:C" & LastRow)
        If cell.Value = 0 Then
            cell.EntireRow.Hidden = True
        End If
    Next cell
    Application.ScreenUpdating = True
End Sub
If the rows are hidden, how do you change the values in column C from zero to something else?


All the row values link to some other cells. Consider the C row as a Total Cost (=Unit Cost * Quantities). I have years from 2013-2022. Each year I have different quantities and unit cost, so the cells vary according to the years.

I am trying the code now.

Thank you.
 
Upvote 0
Try:
Code:
Sub HideRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim cell As Range
    ActiveSheet.UsedRange.Rows.EntireRow.Hidden = False
    For Each cell In Range("C2:C" & LastRow)
        If cell.Value = 0 Then
            cell.EntireRow.Hidden = True
        End If
    Next cell
    Application.ScreenUpdating = True
End Sub
If the rows are hidden, how do you change the values in column C from zero to something else?

It works perfect, and I put a button on it. After I have done the calculation, I will click the button to get the summary.

Thanks a lot.
 
Upvote 0

Forum statistics

Threads
1,215,460
Messages
6,124,949
Members
449,198
Latest member
MhammadishaqKhan

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