Solved

delete rows that don't match multiple criteria

Posted on 2012-03-31
10
656 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel conditional formatting based on 'zero value' 6 20
Delete all empty columns using VBA 7 42
VLOOKUP 6 17
powershell script 9 26
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
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…

809 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