Most Effective Multi-Column Delete Row Filter [VBA]

DataBlake

Well-known Member
Joined
Jan 26, 2015
Messages
781
Office Version
  1. 2016
Platform
  1. Windows
what would be the most effective way i could filter every column for the same criteria? so far i can think of the below sub i made, but it doesnt seem to be that efficient if multiple columns contain the criteria. If you could help me to understand another method i would greatly appreciate the help.

Code:
Sub test()
    Dim p As Long
    Dim lastCol As Long
    
    lastCol = Range("A1").SpecialCells(xlCellTypeLastCell).Column
    
    For p = 1 To lastCol
        With ActiveSheet.Range("A1")
            
            .AutoFilter Field:=p, Criteria1:="=*Trailer*", Criteria2:="=*Dually*", Operator:=xlOr
            On Error GoTo 0
            .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
            
            AutoFilterMode = False
        End With
        Next p
        
    End Sub
 
You could, but it may not be the best way. For example, if you 500 criteria it would make for a very long code. :eek:
So about how many criteria might you have?

your solution was perfect, its super fast.
I only had 5 criteria.
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
your solution was perfect, its super fast.
I only had 5 criteria.
Cheers. Thanks for the follow-up. :)

A more compact way to add your multiple conditions might be to replace the If...ElseIf....ElseIf type structure with this

Rich (BB code):
For i = 1 To UBound(a)
  For j = 1 To uba2
    If Not IsError(a(i, j)) Then
      Select Case True
        Case InStr(1, a(i, j), "trailer", 1) > 0, _
              InStr(1, a(i, j), "dually", 1) > 0, _
              InStr(1, a(i, j), "abcde", 1) > 0
          b(i, 1) = 1
          k = k + 1
          Exit For
      End Select
    End If
  Next j
Next i
 
Upvote 0

Forum statistics

Threads
1,214,630
Messages
6,120,634
Members
448,973
Latest member
ChristineC

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