Solved

delete rows that don't match multiple criteria

Posted on 2012-03-31
10
662 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 47

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 47

Expert Comment

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

739 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