Filter a column using VBA with multiple conditions

sobeitjedi

Board Regular
Joined
Mar 13, 2006
Messages
235
Office Version
  1. 365
Hi.

I have a line within a macro which isn't working:

Selection.AutoFilter field:=3, Operator:=xlFilterValues, Criteria1:=Array("*gala*", "*onep*", "*ipho*")

I'm trying to filter a column to show all results where onep, ipho or gala is in the cell in the column

How do I correct?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi.

I have a line within a macro which isn't working:

Selection.AutoFilter field:=3, Operator:=xlFilterValues, Criteria1:=Array("*gala*", "*onep*", "*ipho*")

I'm trying to filter a column to show all results where onep, ipho or gala is in the cell in the column

How do I correct?


Try this

Code:
Sub Filter_with_multiple_conditions()
    Dim r As Range, f As Range, cell As String, n As Long, i As Long
    Dim crit() As Variant, arr() As Variant
    
    crit = Array("*gala*", "onep", "ipho") 'Enter criteria with or without "*"
    Set r = ActiveSheet.UsedRange
    r.AutoFilter
    n = 0
    For i = 0 To UBound(crit)
        Set f = r.Find(crit(i), LookIn:=xlValues, lookat:=xlPart)
        If Not f Is Nothing Then
            cell = f.Address
            Do
                ReDim Preserve arr(n)
[COLOR=#0000ff]                arr(n) = f.Value[/COLOR]
                n = n + 1
                Set f = r.FindNext(f)
            Loop While Not f Is Nothing And f.Address <> cell
        End If
    Next
[COLOR=#0000ff]    r.AutoFilter Field:=3, Criteria1:=arr, Operator:=xlFilterValues[/COLOR]
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,472
Members
449,087
Latest member
RExcelSearch

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