Solved

delete rows that don't match multiple criteria

Posted on 2012-03-31
10
658 Views
Last Modified: 2012-03-31
Hello,
I need vba code for Excel that will delete all the rows in column A that don't match SOUTHEAST and delete all rows in column B that match GA, NC/SC and TN/KY. I have included a smaple file, thanks this has been driving me crazy!
TEST.xls
0
Comment
Question by:bellboy2k
  • 5
  • 3
  • 2
10 Comments
 
LVL 17

Expert Comment

by:vb_elmar
ID: 37790936
This deletes the SOUTHEAST entries:

Private Sub CommandButton1_Click()

i = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
For r = 2 To i - 1
If Cells(r, 1).Value <> "SOUTHEAST" Then Cells(r, 1).Value = "deleted"
Next

End Sub
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 37790940
This deletes the column B entries:

Private Sub CommandButton1_Click()

i = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row

For r = 2 To i - 1
ole2 = Cells(r, 2).Value

If InStr(1, ole2, "GA") Then Cells(r, 2).Value = "DELETE"
If InStr(1, ole2, "NC/SC") Then Cells(r, 2).Value = "DELETE"
If InStr(1, ole2, "TN/KY") Then Cells(r, 2).Value = "DELETE"
Next

End Sub
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 37790971
This deletes the **ENTIRE** row :

Private Sub CommandButton1_Click()

i = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
For r = i - 1 To 2 Step -1
If Cells(r, 1).Value <> "SOUTHEAST" Then Rows(r).Delete
Next

End Sub
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 17

Expert Comment

by:vb_elmar
ID: 37790977
This deletes the ENTIRE row :

Private Sub CommandButton1_Click()

i = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row

For r = i - 1 To 2 Step -1

ole2 = Cells(r, 2).Value

If InStr(1, ole2, "GA") Then Rows(r).Delete
If InStr(1, ole2, "NC/SC") Then Rows(r).Delete
If InStr(1, ole2, "TN/KY") Then Rows(r).Delete

Next

End Sub
0
 

Author Comment

by:bellboy2k
ID: 37790978
Thanks, but it should delete all regions except SOUTHEAST, I ran your code, but it doesn't delete the row, it puts deleted in the cell, instead, it's a good start.
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 37790989
>>it doesn't delete the row
To delete the row take a look at the sample 3 and 4.
0
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 37791007
Private Sub CommandButton1_Click()
Dim i As Long
Dim r As Range

Set r = Range("A1").End(xlDown).Offset(0, 0)

For i = r.Row To 1 Step -1
    If Range("A" & i) <> "SOUTHEAST" Or _
       Range("B" & i) = "GA" Or _
       Range("B" & i) = "NC/SC" Or _
       Range("B" & i) = "TN/KY" Then
       Range("A" & i).EntireRow.Delete
    End If
Next


End Sub
0
 

Author Comment

by:bellboy2k
ID: 37791110
Thanks MartinLiss,
I used your code to get what I needed done! Here is the final solution.

Dim i As Long
Dim r As Range

Set r = Range("A1").End(xlDown).Offset(0, 0)

For i = r.Row To 1 Step -1
    If Range("A" & i) = "WEST" Or _
       Range("A" & i) = "SOUTHWEST " Or _
       Range("A" & i) = "MIDWEST" Or _
       Range("A" & i) = "Undefined" Or _
       Range("A" & i) = "NORTHEAST" Or _
       Range("B" & i) = "GA - ATLANTA" Or _
       Range("B" & i) = "GA - COASTAL GEORGIA" Or _
       Range("B" & i) = "GA - MACON" Or _
       Range("B" & i) = "GA - SOUTH GEORGIA" Or _
       Range("B" & i) = "NC/SC - CHARLESTON" Or _
       Range("B" & i) = "NC/SC - CHARLOTTE" Or _
       Range("B" & i) = "NC/SC - COLUMBIA" Or _
       Range("B" & i) = "NC/SC - GREENSBORO" Or _
       Range("B" & i) = "NC/SC - GREENVILLE" Or _
       Range("B" & i) = "NC/SC - RALEIGH" Or _
       Range("B" & i) = "TN/KY - CHATTANOOGA" Or _
       Range("B" & i) = "TN/KY - EVANSVILLE" Or _
       Range("B" & i) = "TN/KY - KNOXVILLE" Or _
       Range("B" & i) = "TN/KY - LEXINGTON" Or _
       Range("B" & i) = "TN/KY - LOUISVILLE" Or _
       Range("B" & i) = "TN/KY - NASHVILLE" Or _
       Range("B" & i) = "TN/KY - MEMPHIS" Then
       Range("A" & i).EntireRow.Delete
       End If
Next
0
 

Author Closing Comment

by:bellboy2k
ID: 37791113
Solution worked the first time and was quick!
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 37791121
Thanks. Your solution is different than what you asked for however.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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