Excel VBA how can I remove rows where a cell does not meet specific criteria

I have a very large macro which produces an output as shown in DashSample(attached)
I have set the variable CurMoInt to be the number of the report month so the value will be dynamically adjusted monthly.
My goal is to show only the rows that are -.2 times the month or Less and delete rows where the value in Col I is higher than the month times -.2 (negative point 2).
So as an example in February any row with a value .399 or more with be deleted.
I tried the code below without success.
I could really use some help! Thanks

sample view of report
'Remove rows from Dashboard (FY) that don't meet criteria
 HRiskVal = CurMoInt * -0.2
 With Sheets("Dashboard (FY)").Range("I10:I205").Select
     For Each Cell In Selection
        If Cell.Value < HRiskVal Then
               Selection.EntireRow.Select
               Selection.Delete
        End If
    Next Cell
 End With
LVL 1
Mitch SwetskyBusiness AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wshark83Commented:
try this


Sub InsertLine()
    Dim i As Long
    For i = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
        If Cells(i - 1, 1) <> Cells(i, 1) Then   --> change this line to set your criteria
            Rows(i).Delete
        End If
    Next
End Sub
0
Frank WhiteCommented:
Your original code uses a "With" block the wrong way. It also uses .Select and Selection, which is unnecessarily rounadabout and much slower than using direct references. Reworking it, it would give something more like this (untested):

'Remove rows from Dashboard (FY) that don't meet criteria
hriskval = curmoint * -0.2
For Each Cell In Sheets("Dashboard (FY)").Range("I10:I205")
    With Cell
        If .Value < hriskval Then
            .EntireRow.Delete
        End If
    End With
Next Cell

Open in new window


However, here's an alternative that in my tests (on my machine - results may vary) proved to be faster and cleaner than other functions of this type (that I tried):
hriskval = curmoint * -0.2
Dim i As Long, r As Range
Set r = Sheets("Dashboard (FY)").Range("I10:I205")
For i = 1 To r.count
    If r(i).Value < hriskval Then r(i).EntireRow.Delete
Next

Open in new window

0
Mitch SwetskyBusiness AnalystAuthor Commented:
Sorry for the delayed response.
I tried

HRiskVal = CurMoInt * -0.2
Range("I12").Select
     Dim i As Long
    For i = Cells(205, 1).End(xlUp).Row To 12 Step -1
        If Cells(i - 1, 1).Value < HRiskVal Then
            Rows(i).Delete
        End If
    Next

And no rows were deleted.

I know there will only be 202 rows on the sheet so then I changed the count row to 205.
I also noticed that when I hovered of the Cells(i-1,1) it showed what looked like a row number so I tried adding value but that didn't help either.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Mitch SwetskyBusiness AnalystAuthor Commented:
I tried your second suggestion and it deleted rows but it deleted the wrong ones.
For example it deleted a row that had (3.86)

The values I want are the negative ones that are more negative than (CurMoInt * -0.2)
0
Mitch SwetskyBusiness AnalystAuthor Commented:
This is what I tried

hriskval = curmoint * -0.2
For Each Cell In Sheets("Dashboard (FY)").Range("I12:I205")
    With Cell
        If .Value > hriskval Then
            .EntireRow.Delete
        End If
    End With
Next Cell
0
Frank WhiteCommented:
Take note of the mathematical comparison operator symbols.

In your last attempt, you put .Value > hriskval, which will mean "if the value is above hriskval, do this:", so it should work and delete the correct rows if you switch around to .Value < hriskval instead.

Also, keep in mind that all the examples so far assume that every single cell in the range (I12 to I205 in your latest code) is formatted as "General" or "Number" and can only contain decimal numbers. If any of them contains text, blanks, dates or other non-pure-number values, you won't get the desired result out of the code put forward so far.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mitch SwetskyBusiness AnalystAuthor Commented:
Thanks for you quick reply and patience.
I tried both < and > and did not get the results I expected.

The cells are formatted as Number with 2 Dec and there are some 0.00 included.
They were formatted as negative red with parenths so I tried plain -3.79

The range of the current set is -3.79 to 4.15 with approx 25-40 rows that have 0.00s
0
Mitch SwetskyBusiness AnalystAuthor Commented:
I figured out why your suggestion didn't work and thank you for the direction.
The values I wanted to compare were in Col I  so when I changed the col ref it worked.
Thank you again for your assistance.

The following works:
    HRiskVal = CurMoInt * -0.2
    Range("I12:I202").Select
    Dim i As Long
    For i = Cells(202, 9).End(xlUp).Row To 12 Step -1
        If Cells(i - 1, 9) > HRiskVal Then
            Rows(i).Delete
        End If
    Next
0
Mitch SwetskyBusiness AnalystAuthor Commented:
Thanks for your patience
0
wshark83Commented:
seeing as you have used my solution with a few modificiation i think you should split the points accordingly
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.