Find several names at a time

  • Thread starter Thread starter Elby
  • Start date Start date
E

Elby

I'm trying to eliminate records in an Excel database where one field
fits any of several criteria. I've got a code that works for one
instance (in the example below, Dukakis), but I want to look for about
20 names and delete them. Any ideas?

If I were in Excel, I'd use OR, as in
=IF(OR(A1 = "dukakis",A1 = "*smith"),"value if true","value if
false")
But I can't get OR to work in VBA (and I can't search for help in
Google!)

Here's my code so far:

'Remove workflow Worksheets
Range("a1", Range("a1").End(xlDown)).Select
Set RNG = Selection

For Each D In RNG
If D.Value Like "*Dukakis*" Then
D.EntireRow.Delete
End If
Next
 
one way:

Dim vNames As Variant
Dim rCell As Range
Dim rDelete As Range
Dim i As Long

vNames = Array("*Dukakis", "*Smith", "*Jones") 'etc.
For Each rCell In Range("A1:A" & _
Range("A" & Rows.Count).End(xlUp).Row)
With rCell
For i = LBound(vNames) To UBound(vNames)
If .Value Like vNames(i) Then
If rDelete Is Nothing Then
Set rDelete = rCell
Else
Set rDelete = Union(rDelete, rCell)
End If
Exit For
End If
Next i
End With
Next rCell
If Not rDelete Is Nothing Then rDelete.EntireRow.Delete
 
Hmm - my response got deleted in favor of a strange advertisement.
Anyway, just wanted you to know it worked beautifully. Thanks a
million.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top