Hide header row if range of cells in column is 0

jamesyvoi

New Member
Joined
May 9, 2024
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I have a sheet where I am already using a macro (tied to a button) to hide all of the rows in a range that contain 0. I'd also like to be able to hide the "header" row if all cells in that range are 0. If one or more cells is greater than 0 the header row should be shown.

Header row: 18, Range of cells C19:C174, header row 175 range of cells C176:C194, header row 195 range of cells C196:C220.

I haven't been able to find any solutions that address this problem specifically (maybe I'm searching for the wrong thing.

My code for hiding rows is below, maybe I can integrate the 2?

VBA Code:
Sub HideRows()
    
    Dim cell As Range, HideRow As Boolean
    
    With ThisWorkbook.Sheets("QUICK QUOTE")
        For Each cell In .Range("C19:C174,C176:C194,C196:C220,E240:E242").Cells
            With cell
                If IsNumeric(.Value) Then
                    If .Value = 0 Then HideRow = True ' flag to hide
                End If
                If Not .EntireRow.Hidden = HideRow Then ' take action
                    .EntireRow.Hidden = HideRow
                End If
                HideRow = False ' reset for the next iteration
            End With
        Next cell
    End With
    
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
Hi there,

Below is a simplyfied copy of my spreadsheet. I am trying to hide the Rows that say "Island Tables, Market Tables, etc" if all of the items below them have a qty of 0.

Book1
ABCDE
1LineDescriptionQuantityPriceExtended Price
2ISLAND TABLES
317.5ft Classic Island Table Package0$10.00$0.00
42- Ridge Divider Package0$10.00$0.00
53- Dual Lower Shelf Upgrade0$10.00$0.00
64- Hybrid End Upgrade0$10.00$0.00
75- Angled Upper Shelf0$10.00$0.00
867.5ft Harvest Island Table Package0$10.00$0.00
97- Ridge Divider Package0$10.00$0.00
108- Flat Upper Shelf0$10.00$0.00
1197.5ft Slim Classic Island Table0$10.00$0.00
1210- Ridge Divider Package0$10.00$0.00
1311- Dual Lower Shelf Upgrade0$10.00$0.00
1412- Flat Upper Shelf0$10.00$0.00
15WALL TABLES
1614ft Classic Wall Table0$10.00$0.00
1725ft Classic Wall Table0$10.00$0.00
1836.5ft Classic Wall Table0$10.00$0.00
1948ft Classic Wall Table0$10.00$0.00
2059ft Classic Wall Table0$10.00$0.00
21610.5ft Classic Wall Table0$10.00$0.00
22712ft Classic Wall Table0$10.00$0.00
23812.5ft Classic Wall Table0$10.00$0.00
24914ft Classic Wall Table0$10.00$0.00
251014.5ft Classic Wall Table0$10.00$0.00
261115ft Classic Wall Table0$10.00$0.00
271216ft Classic Wall Table0$10.00$0.00
281316.5ft Classic Wall Table0$10.00$0.00
291420ft Classic Wall Table0$10.00$0.00
301522ft Classic Wall Table0$10.00$0.00
311623ft Classic Wall Table0$10.00$0.00
321724ft Classic Wall Table0$10.00$0.00
331832ft Classic Wall Table0$10.00$0.00
341940ft Classic Wall Table0$10.00$0.00
35MARKET TABLES
3620Classic 4x2.5 Market Table0$10.00$0.00
3721Classic 4x3 Market Table0$10.00$0.00
3822Classic 4x3 Market Table w/Island Style Dividers0$10.00$0.00
3923Classic 4x3 Market Table w/Ice Drain Kit & Side Panels0$10.00$0.00
4024Contemporary 4x3 Market Table0$10.00$0.00
4125Harvest 4x3 Market Table0$10.00$0.00
4226Classic 5x3 Market Table0$10.00$0.00
4327Classic 5x3 Market Table w/Island Style Dividers0$10.00$0.00
4428Classic 5x3 Market Table w/Ice Drain Kit & Side Panels0$10.00$0.00
4529Contemporary 5x3 Market Table0$10.00$0.00
4630Harvest 5x3 Market Table0$10.00$0.00
4731$0.00
4832$0.00
49Subtotal$0.00
50Discount#REF!
51Installation#REF!
52Shipping#REF!
53#REF!#REF!
Sheet1
Cell Formulas
RangeFormula
A3:A14A3=AGGREGATE(2,5,A2:A$14)+1
E3:E14,E36:E48,E16:E34E3=C3*D3
A16:A34,A36:A48A16=AGGREGATE(2,5,A$15:A15)+1
E49E49=SUM(E3:E14,E16:E34,E36:E48)
E50E50=IF(COUNTBLANK(#REF!),"",E49*(#REF!/100))
E51:E52E51=#REF!
D53D53=CONCAT("Total ",#REF!)
E53E53=IF(ISBLANK(E50),E49,E49+E50+E51+E52)
 
Upvote 0
Try:
VBA Code:
Sub HideRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, i As Long, fRow As Long, lRow As Long
    LastRow = Range("B" & Rows.Count).End(xlUp).Row
    With Range("B3:B" & LastRow).SpecialCells(xlCellTypeConstants)
        For i = .Areas.Count To 1 Step -1
            fRow = .Areas(i).Cells(1).Row
            lRow = .Areas(i).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            If WorksheetFunction.CountIf(Range("C" & fRow & ":C" & lRow), "0") = lRow - fRow + 1 Then
                Range("C" & fRow - 1 & ":C" & lRow).EntireRow.Hidden = True
            Else
                With Range("C" & fRow - 1 & ":C" & lRow)
                    On Error Resume Next
                    .Replace "0", "#N/A", xlWhole, , False
                    .SpecialCells(xlConstants, xlErrors).EntireRow.Hidden = True
                    .SpecialCells(xlConstants, xlErrors) = 0
                    On Error GoTo 0
                End With
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you,

This works well, however if I populate any of the fields in the Quantity or Price columns using a formula (to pull data from another sheet) then it wont hide any of the rows under that category (Island Tables, etc), even if they are at 0.
 
Upvote 0
Try replacing:
VBA Code:
.SpecialCells(xlConstants, xlErrors).EntireRow.Delete
with
VBA Code:
.SpecialCells(xlFormulas, xlErrors).EntireRow.Delete
 
Upvote 0
That didn't make a difference unfortunately (you said .EntireRow.Delete, but I assumed you meant the line with .EntireRow.Hidden = True)
 
Upvote 0
Oops. Should be EntireRow.Hidden=True. The macro works for me. Does it work for you?
 
Upvote 0
No, it doesn't work for me when there is a formula that is referencing a different cell. I've attached an updated copy below to show what I mean.

Book1
ABCDEFG
1LineDescriptionQuantityPriceExtended Price
2ISLAND TABLES1
317.5ft Classic Island Table Package1$2.00$2.002
42- Ridge Divider Package1$10.00$10.003
53- Dual Lower Shelf Upgrade1$10.00$10.00
64- Hybrid End Upgrade#N/A$10.00#N/A
75- Angled Upper Shelf#N/A$10.00#N/A
867.5ft Harvest Island Table Package#N/A$10.00#N/A
97- Ridge Divider Package#N/A$10.00#N/A
108- Flat Upper Shelf#N/A$3.00#N/A
1197.5ft Slim Classic Island Table#N/A$10.00#N/A
1210- Ridge Divider Package#N/A$10.00#N/A
1311- Dual Lower Shelf Upgrade#N/A$10.00#N/A
1412- Flat Upper Shelf#N/A$10.00#N/A
15WALL TABLES
16134ft Classic Wall Table0$10.00$0.00
17135ft Classic Wall Table0$10.00$0.00
18146.5ft Classic Wall Table0$10.00$0.00
19158ft Classic Wall Table0$10.00$0.00
20169ft Classic Wall Table0$10.00$0.00
211710.5ft Classic Wall Table0$10.00$0.00
221812ft Classic Wall Table0$10.00$0.00
231912.5ft Classic Wall Table0$10.00$0.00
242014ft Classic Wall Table0$10.00$0.00
252114.5ft Classic Wall Table0$10.00$0.00
262215ft Classic Wall Table0$10.00$0.00
272316ft Classic Wall Table0$10.00$0.00
282416.5ft Classic Wall Table0$10.00$0.00
292520ft Classic Wall Table0$10.00$0.00
302622ft Classic Wall Table0$10.00$0.00
312723ft Classic Wall Table0$10.00$0.00
322824ft Classic Wall Table0$10.00$0.00
332932ft Classic Wall Table0$10.00$0.00
343040ft Classic Wall Table0$10.00$0.00
35MARKET TABLES
3631Classic 4x2.5 Market Table0$10.00$0.00
3732Classic 4x3 Market Table0$10.00$0.00
3832Classic 4x3 Market Table w/Island Style Dividers0$10.00$0.00
3933Classic 4x3 Market Table w/Ice Drain Kit & Side Panels0$10.00$0.00
4034Contemporary 4x3 Market Table0$10.00$0.00
4135Harvest 4x3 Market Table0$10.00$0.00
4236Classic 5x3 Market Table0$10.00$0.00
4337Classic 5x3 Market Table w/Island Style Dividers0$10.00$0.00
4438Classic 5x3 Market Table w/Ice Drain Kit & Side Panels0$10.00$0.00
4539Contemporary 5x3 Market Table0$10.00$0.00
4640Harvest 5x3 Market Table0$10.00$0.00
47410$0.00
48420$0.00
49Subtotal#N/A
50Discount#REF!
51Installation#REF!
52Shipping#REF!
53#REF!#N/A
Sheet1
Cell Formulas
RangeFormula
D3D3=G3
C3C3=G2
C4C4=G2
C5C5=G2
D10D10=G4
A3:A14A3=AGGREGATE(2,5,A$1:A2)+1
E3:E14,E36:E48,E16:E34E3=C3*D3
A16:A34A16=AGGREGATE(2,5,A$1:A14)+1
A36:A48A36=AGGREGATE(2,5,A$1:A33)+1
E49E49=SUM(E3:E14,E16:E34,E36:E48)
E50E50=IF(COUNTBLANK(#REF!),"",E49*(#REF!/100))
E51:E52E51=#REF!
D53D53=CONCAT("Total ",#REF!)
E53E53=IF(ISBLANK(E50),E49,E49+E50+E51+E52)
 
Upvote 0
Try with a little modification to Mumps code :

VBA Code:
Sub HideRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, i As Long, fRow As Long, lRow As Long
    LastRow = Range("C" & Rows.Count).End(xlUp).Row
    With Range("C3:C" & LastRow).SpecialCells(xlCellTypeConstants)
        For i = .Areas.Count To 1 Step -1
            fRow = .Areas(i).Cells(1).Row
            lRow = .Areas(i).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            If WorksheetFunction.CountIf(Range("C" & fRow & ":C" & lRow), "0") = lRow - fRow + 1 Then
                Range("C" & fRow - 1 & ":C" & lRow).EntireRow.Hidden = True
            Else
                With Range("C" & fRow - 1 & ":C" & lRow)
                    On Error Resume Next
                    .Replace "0", "#N/A", xlWhole, , False
                    .SpecialCells(xlConstants, xlErrors).EntireRow.Hidden = True
'                    .SpecialCells(xlConstants, xlErrors) = 0
                    On Error GoTo 0
                End With
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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