Hide each row in a range with a 0 sum.

racastor

New Member
Joined
Sep 18, 2015
Messages
8
I'm attempting to look at a user defined range to see which rows sum to zero and then hide those rows. It hides the first row, but doesn't move on to the next rows. Where did I go wrong? Using Excel 2016.

Code:
Dim rng As Range
Dim row As Range
Dim cell As Range
Dim FRow As Integer
Dim LRow As Integer


FRow = InputBox("Enter the first row of the reconciliation.")
LRow = InputBox("Enter the last row of the reconciliation.")


Set rng = Range(Cells(FRow, 4), Cells(LRow, 9))


For Each row In rng.Rows
  
    If WorksheetFunction.Sum(row) = 0 Then
    Selection.EntireRow.Hidden = True
    Else
    End If
Next row
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You are telling to hide the selected row. Since your code isn't selecting anything, it is always hiding whatever row was selected when you started.

Change this row:
Code:
    Selection.EntireRow.Hidden = True
to this:
Code:
    Rows(row).Hidden = True
 
Last edited:
Upvote 0
Ah, yes. That is because you have defined "row" as a range. So we need to take the row number of that.
That would look like:
Code:
row.row
However, that is bad form. You should avoid using reserved words like "row" as the name of your variables. Doing so can cause ambiguity, unexpected behavior, and errors in your code.

Try this:
Code:
Dim rng As Range
Dim rw As Range
Dim cell As Range
Dim FRow As Integer
Dim LRow As Integer


FRow = InputBox("Enter the first row of the reconciliation.")
LRow = InputBox("Enter the last row of the reconciliation.")


Set rng = Range(Cells(FRow, 4), Cells(LRow, 9))


For Each rw In rng.Rows
  
    If WorksheetFunction.Sum(rw) = 0 Then
        Rows(rw.row).Hidden = True
    End If
Next rw
 
Last edited:
Upvote 0
You are welcome!

Avoiding reserved words seems like a good Best Practice I'll keep in mind.
Yes. If I am ever not sure if a word is reserved or not, I will just prefix it with "My", just to be safe.
 
Upvote 0

Forum statistics

Threads
1,223,628
Messages
6,173,426
Members
452,515
Latest member
Alicedonald9

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