autofilter not equal to multiple criterias

TheMadTree

New Member
Joined
Mar 23, 2015
Messages
25
Good afternoon!

I am looking for a way to filter/delete everything out of a column besides 5 strings of text.
I tried looping through the 8000+ rows evaluating if the string of text isn't one I want to keep, but the execution of this code takes far too long:

For i = LastRow To 2 Step -1
If Cells(i, 1) <> "text1" And Cells(i, 1) <> "text2" And Cells(i, 1) <> "text3" And Cells(i, 1) <> "text4" And Cells(i, 1) <> "text5" Then Rows(i).Delete
Next

And I can't seem to auto filter everything out I don't want to keep as we're only entitled to two criteria.
(Filtering out all other values besides the 5 string of text I want to keep with and array is not an option as they're too many and can vary)

Does anyone have a possible solution and/or workaround?

Thank you,

Elias
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
A possible workaround

Create a helper column with the formula
=ISERROR(MATCH(A2,{"text1";"text2";"text3";"text4";"text5"},0))
and filter the data using this column as criterion

Hope this helps

M.
 
Upvote 0
This is a vba solution:
Code:
Sub a939630a()
Dim va, i As Long, r As Range
Set r = Range("A2", Cells(Rows.count, "A").End(xlUp))
va = r.Value
For i = 1 To UBound(va, 1)
    If va(i, 1) <> "text1" And va(i, 1) <> "text2" And va(i, 1) <> "text3" And va(i, 1) <> "text4" And va(i, 1) <> "text5" Then
         va(i, 1) = ""
    End If
Next
r.ClearContents
Range("A2").Resize(UBound(va, 1), 1) = va
r.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,934
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top