Solved

delete rows that don't match multiple criteria

Posted on 2012-03-31
10
652 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
>>it doesn't delete the row
To delete the row take a look at the sample 3 and 4.
0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Solution worked the first time and was quick!
0
 
LVL 45

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

763 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

9 Experts available now in Live!

Get 1:1 Help Now