Multiple criteria on filter

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
112
Office Version
  1. 2019
Platform
  1. Windows
I am trying to filter out data by having the criteria: on column D, it should contain either X100 or X101 and data on column E should not contain any of the ff: G1, G2, G3 but the code is having error when I run it.

VBA Code:
Sub FilterData
    Dim LR As Long

'
    Sheets(1).Activate
    LR = Range("A" & Rows.Count).End(xlUp).Row

   
    ActiveSheet.Range("A1:D" & LR).AutoFilter Field:=4, Criteria1:="=X101", _
        Operator:=xlOr, Criteria2:="=X100"
    Range("A1").Select
    ActiveSheet.Range("A1:D" & LR).AutoFilter Field:=5, Criteria1:="<>*G1*" _
        , Operator:=xlAnd, Criteria2:="<>*G2*", Operator:=xlAnd, Criteria3:="<>*G3*"
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You can only have 1 or 2 criteria, the parameter Criteria3 is not valid.

Your range is set to A1:D & lr (4 columns) so there is no Field / column 5.

The easiest way to achieve what you want is with a helper column to filter on. Assuming column F is empty, enter the formula below in F2, array confirm it with Ctrl Shift Enter and fill down, then filter to show only rows which are TRUE in that column.

=AND(OR(D2={"X100","X101"}),ISERROR(SEARCH({"G1","G2","G3"},E2)))

If desired, you could capture the formula entry with the macro recorder, then use that with your filter code to enter the formula when needed.
 
Upvote 0
ActiveSheet.Range("A1:D" & LR).AutoFilter Field:=5, Criteria1:="<>*G1*" _ , Operator:=xlAnd, Criteria2:="<>*G2*", Operator:=xlAnd, Criteria3:="<>*G3*"
As Jason comments, that way you can only filter up to 2 criteria ..
But you can put the data that does not contain G1, G2 and G3 in an array and filter by values:

Remember to put the range up to column E to filter by column 5.

VBA Code:
Sub FilterData()
    Dim lr As Long, i As Long
    Dim dic As Object
    
    Set dic = CreateObject("Scripting.Dictionary")
    Sheets(1).Activate
    If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
    lr = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To lr
      If Not Range("E" & i) Like "*G1*" And Not Range("E" & i) Like "*G2*" And _
         Not Range("E" & i) Like "*G3*" Then
          dic(Range("E" & i).Value) = Empty
      End If
    Next
    ActiveSheet.Range("A1:E" & lr).AutoFilter 4, "=X101", xlOr, "=X100"
    ActiveSheet.Range("A1:E" & lr).AutoFilter 5, dic.keys, xlFilterValues
End Sub
 
Upvote 0
As Jason comments, that way you can only filter up to 2 criteria ..
But you can put the data that does not contain G1, G2 and G3 in an array and filter by values:

Remember to put the range up to column E to filter by column 5.

VBA Code:
Sub FilterData()
    Dim lr As Long, i As Long
    Dim dic As Object
   
    Set dic = CreateObject("Scripting.Dictionary")
    Sheets(1).Activate
    If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
    lr = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To lr
      If Not Range("E" & i) Like "*G1*" And Not Range("E" & i) Like "*G2*" And _
         Not Range("E" & i) Like "*G3*" Then
          dic(Range("E" & i).Value) = Empty
      End If
    Next
    ActiveSheet.Range("A1:E" & lr).AutoFilter 4, "=X101", xlOr, "=X100"
    ActiveSheet.Range("A1:E" & lr).AutoFilter 5, dic.keys, xlFilterValues
End Sub
May I just ask what is the function of this line
VBA Code:
  Set dic = CreateObject("Scripting.Dictionary")
Im not really that well versed on using excel vba thanks! ?
 
Upvote 0
May I just ask what is the function of this line
VBA Code:
Set dic = CreateObject("Scripting.Dictionary")

In this case the macro uses it to store the unique values in column E that do not meet the criteria.
Here more information about the vba-dictionary:

 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,684
Members
448,977
Latest member
dbonilla0331

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