Solved

delete rows that don't match multiple criteria

Posted on 2012-03-31
10
653 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now