Require help with VBA code for hiding rows based on cells in range of columns being 0

MonicaP

New Member
Joined
Jan 12, 2022
Messages
6
Office Version
  1. 2010
Platform
  1. Windows
Require help with VBA code for hiding rows based on cells in range of columns being 0
I'm found this code to use but when I use it, it hides all the rows. I need it to hide row if all cells in columns H through L show a 0 value. Can someone please help me with this.

Sub Hide_rows()

Dim LastRow As Long

Dim Rng As Range

LastRow = Range("A125").End(xlUp).Row '

Set Rng = Range("H3:L125")

Application.ScreenUpdating = False

For Each cell In Rng

If cell.Value = "0" Then

cell.EntireRow.Hidden = True

End If

Next cell

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Welcome to the Board!

Does the possibility exist for blanks (empty cells) to be included in the data?
If so, are they to be treated as zeroes or not?
 
Upvote 0
Welcome to the Board!

Does the possibility exist for blanks (empty cells) to be included in the data?
If so, are they to be treated as zeroes or not?
No, there would be a zero in the cell. It's formula based.
 
Upvote 0
OK, try this:
VBA Code:
Sub MyHideRows()

    Dim r As Long
    Dim rng As Range
    
    Application.ScreenUpdating = False
    
'   Loop through all rows
    For r = 3 To 125
'       Set range to check
        Set rng = Range(Cells(r, "H"), Cells(r, "L"))
'       Count the number of zeroes in range
        If Application.WorksheetFunction.CountIf(rng, "=0") = 5 Then
'           Hide rows
            Rows(r).Hidden = True
        Else
            Rows(r).Hidden = False
        End If
    Next r

    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
OK, try this:
VBA Code:
Sub MyHideRows()

    Dim r As Long
    Dim rng As Range
   
    Application.ScreenUpdating = False
   
'   Loop through all rows
    For r = 3 To 125
'       Set range to check
        Set rng = Range(Cells(r, "H"), Cells(r, "L"))
'       Count the number of zeroes in range
        If Application.WorksheetFunction.CountIf(rng, "=0") = 5 Then
'           Hide rows
            Rows(r).Hidden = True
        Else
            Rows(r).Hidden = False
        End If
    Next r

    Application.ScreenUpdating = True

End Sub
Hi, sorry, this didn't hide anything. When I look at the Range (Cells(r, "H"), Cells(r, "L")) does that cover the columns in between? I'm trying to understand. I do appreciate your help. My sheet is actually over 11000 rows and I wanted to test on a small area first.
 
Upvote 0
When I look at the Range (Cells(r, "H"), Cells(r, "L")) does that cover the columns in between?
Yes it does.

Can you post a few things:
1. One of the formulas that is in these cells
2. A small sample of your data

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Yes it does.

Can you post a few things:
1. One of the formulas that is in these cells
2. A small sample of your data

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
LOCATION 1 RECONCILIATION 2022.xlsx
ABCEFGHIJKLMNO
1INSTRUCTIONS HERE ARE FOR WHEN ALL BILLING HAS BEEN COMPLETED AND ALL INVENTORY ADJUSTMENTS HAVE BEEN MADE.IMPORT AVG COST FOR LOCATIONIMPORT QTY AND VALUES FOR LOCATION
2CODEITEMNODESCCNTLACCTSTOCK UNITAccpac COSTTotal CountedCount ValueAccpac QTYAccpac TL VALUEvariance QTYvariance VALUE
310001008CRVGELEUROSEAL GEL (148 ml)1EA15000150
41000204615STRUCTURAL ADH 1MIN4EA000000
51000304616STRUCTURAL ADH 5 MIN4EA18000180
61000404617STRUCTURAL ADH 15 MIN4EA000000
71000504618STRUCTURAL ADH 40 MIN4EA100010
81000604620SEAM SEALER 10 MIN1EA15000150
91000704621SELF LEVEL SEAM SLR4EA000000
101000804622URETHANE FOAM 2 PART4EA600060
111000904623PLASTIC PARTS CLEANER4EA000000
121001004624TPO ADH PRMTR BRUSH TOP1EA000000
131001104625TPO ADH PRMTR AEROSOL1EA000000
141001204626DBL CART APPLICATOR GUN1EA000000
151001304627MIXING NOZZLES 18 ELM4EA000000
161001404628MIXING NOZZLES 24 ELM4EA000000
171001504628OUL24 ELEMENT NOZZLES (GREEN INDIVIDUAL)3EA000000
181001604631STRUCT BACKING PATCH4EA000000
191001704632FIBERGLASS RNFRCG TAPE1EA000000
201001804633PLSTIC CONTR RELS FLM4EA000000
211001904634STRUCTURAL ADH START KIT4EA000000
221002004647STRUCTURAL ADHESIVE 30 SECOND4EA000000
231002104671SMC FIBERGLASS URETHANE ADH 400ML 3MIN4EA000000
241002204672SMC FIBERGLASS URETHANE ADH 10 MINUTE4EA000000
MASTER
Cell Formulas
RangeFormula
H3:H24H3=SUMIF(ENTRY!B:C,MASTER!$A3,ENTRY!C:C)
I3:I24I3=H3*G3
N3:O24N3=H3-K3


i hope this works
 
Upvote 0
The first thing I asked is if there is the possibility of blank cells in the range, and you said "No".
But all of column J is blank!

So even though that spans 5 columns of data, only 4 would ever have zeroes in them.
Therefore, you would need to change this line to:
Rich (BB code):
        If Application.WorksheetFunction.CountIf(rng, "=0") = 5 Then
to this:
Rich (BB code):
        If Application.WorksheetFunction.CountIf(rng, "=0") = 4 Then
 
Upvote 0
The first thing I asked is if there is the possibility of blank cells in the range, and you said "No".
But all of column J is blank!

So even though that spans 5 columns of data, only 4 would ever have zeroes in them.
Therefore, you would need to change this line to:
Rich (BB code):
        If Application.WorksheetFunction.CountIf(rng, "=0") = 5 Then
to this:
Rich (BB code):
        If Application.WorksheetFunction.CountIf(rng, "=0") = 4 Then
The first thing I asked is if there is the possibility of blank cells in the range, and you said "No".
But all of column J is blank!

So even though that spans 5 columns of data, only 4 would ever have zeroes in them.
Therefore, you would need to change this line to:
Rich (BB code):
        If Application.WorksheetFunction.CountIf(rng, "=0") = 5 Then
to this:
Rich (BB code):
        If Application.WorksheetFunction.CountIf(rng, "=0") = 4 Then
My apologies, yes, I missed that. Your solution worked. Thank you very much. I appreciate your help!
 
Upvote 0
You are welcome.
Glad I was able to help!
:)

You can see why I asked it now. That little detail can mean the difference between working and not working.
It is never a bad idea to post a small sample of your data so we can see that, or other little things that may be important.
Many times, the devil is in the details!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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