Solved

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

Posted on 2012-04-02
10
286 Views
Last Modified: 2012-04-05
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
0
Comment
Question by:Mswetsky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
10 Comments
 
LVL 6

Expert Comment

by:wshark83
ID: 37796170
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
 
LVL 3

Expert Comment

by:Frank White
ID: 37796590
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
 
LVL 1

Author Comment

by:Mswetsky
ID: 37797173
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 1

Author Comment

by:Mswetsky
ID: 37797238
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
 
LVL 1

Author Comment

by:Mswetsky
ID: 37797247
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
 
LVL 3

Accepted Solution

by:
Frank White earned 500 total points
ID: 37797638
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
 
LVL 1

Author Comment

by:Mswetsky
ID: 37797794
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
 
LVL 1

Author Comment

by:Mswetsky
ID: 37798087
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
 
LVL 1

Author Closing Comment

by:Mswetsky
ID: 37798090
Thanks for your patience
0
 
LVL 6

Expert Comment

by:wshark83
ID: 37806847
seeing as you have used my solution with a few modificiation i think you should split the points accordingly
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question