Excel Macro - search for multiple values

I  am trying to search column A for value AAS or ACC and if I find them keep those rows and delete everything else.  the code I'm pasting works fine if there is only 1 value  either AAS or ACC - so it is the OR component I'm having an issue with.  Any help would be appreciated.  

 Dim rng As Range, cell As Range, del As Range
 Set rng = Intersect(Range("A:A"), ActiveSheet.UsedRange)
 For Each cell In rng
   If (cell.Value) <> "AAS" Or (cell.Value) <> "ACC" Then
       If del Is Nothing Then
          Set del = cell
       Else: Set del = Union(del, cell)
       End If
     
    End If
 Next cell
 On Error Resume Next
 del.EntireRow.Delete
jenitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
Try this change:

    If Not ((cell.Value) = "AAS" Or (cell.Value) = "ACC") Then


~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
I hate Not logic.

 
If (cell.Value) = "AAS" Or (cell.Value) = "ACC" Then
    ' Do nothing
Else
       If del Is Nothing Then
          Set del = cell
      Else
        Set del = Union(del, cell)
      End If
End If

Open in new window

0
Bill PrewCommented:
And a slightly more maintainable approach, making it easier to change the acceptable values to keep.

Dim rng As Range, cell As Range, del As Range
Dim KeepValues As String
KeepValues = "AAS,ACC"
Set rng = Intersect(Range("A:A"), ActiveSheet.UsedRange)
For Each cell In rng
    If InStr(1, "," & KeepValues & ",", "," & cell.Value & ",", vbTextCompare) = 0 Then
        If del Is Nothing Then
            Set del = cell
        Else
            Set del = Union(del, cell)
        End If
    End If
Next cell
On Error Resume Next
del.EntireRow.Delete

Open in new window

~bp
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Saurabh Singh TeotiaCommented:
One of the possible reasons is that in VB when you compare texts it compare case sensitivity..The way you set it up it doesn't check for that..the way i will do this to ensure no matter what the case it does it job was like this..

Dim rng As Range, cell As Range, del As Range
 Set rng = Intersect(Range("A:A"), ActiveSheet.UsedRange)
 For Each cell In rng
   If InStr(1, cell.Value, "aas", vbTextCompare) = 0 And InStr(1, cell.Value, "acc", vbTextCompare) = 0 Then
       If del Is Nothing Then
          Set del = cell
       Else: Set del = Union(del, cell)
       End If
     
    End If
 Next cell
 On Error Resume Next
 del.EntireRow.Delete

Open in new window


Saurabh...
0
Rob HensonFinance AnalystCommented:
In the original, if you change the OR for an AND, it will solve the problem.

This means it doesn't equal "ASC"  AND it doesn't equal "ACC" so it must be something else.
0
jenitAuthor Commented:
Simple and easy.  Thanks all.  I didn't try all the solutions as simply adding the NOT, () and change <> to = worked.   Sorry it took so long for me to get back.  We had a major client issue that put all other things on total HOLD.  

All is resolved now including my macro challenge.  

LOVE the help available out here!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.