VBA to AutoFilter with Multiple Criteria on Same Field in Excel: 4 Methods

Method 1 – Embed VBA to AutoFilter with Multiple Numeric Criteria on the Same Column

Steps:

  • Press Alt + F11 on your keyboard or go to the tab Developer -> Visual Basic to open Visual Basic Editor.

  • In the pop-up code window, from the menu bar, click Insert -> Module.

  • Copy the following code and paste it into the code window.
Sub AutoFilterWithMultipleCriteriaOnSameColumn()
Dim iArray As Variant
With ThisWorkbook.Worksheets("Column")
    iArray = Split(Join(Application.Transpose(.Range(.Cells(5, 4), .Cells(.Range("D:D").Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row, 4)).Value)))
    .Range("B4").AutoFilter Field:=1, Criteria1:=iArray, Operator:=xlFilterValues
End With
End Sub

Your code is now ready to run.

VBA to AutoFilter with Multiple Numeric Criteria on Same Field in Excel

  • Press F5 on your keyboard, or from the menu bar select Run -> Run Sub/UserForm. You can also just click on the Run icon in the sub-menu bar to run the macro.

After successful code execution, look at the following image to check out the result.

Result of VBA to AutoFilter with Multiple Numeric Criteria on Same Field in Excel

Column B is now filtered with only the odd numbers.

Perform the same code to filter data based on even numbers. You have to store even numbers in another column instead of odd numbers.

VBA Code Explanation

Dim iArray As Variant

Define the variable for the array.

With ThisWorkbook.Worksheets("Column")

Declare the worksheet name (“Column” is the sheet name for our dataset). You must write the sheet name according to your dataset.

iArray = Split(Join(Application.Transpose(.Range(.Cells(5, 4), .Cells(.Range("D:D").Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row, 4)).Value)))

Fill the defined array with the criteria stored in Column D starting on Cell D5.

.Range("B4").AutoFilter Field:=1, Criteria1:=iArray, Operator:=xlFilterValues
End With
  • Filter Column B starts on Cell B4 according to the multiple criteria stored in the defined array.
  • Leave the worksheet.

Method 2 – Implement VBA to AutoFilter with AND Operator on Same Column in Excel

Steps:

  • Open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Copy the following code and paste it into the code window.
Sub AutoFilterOnSameColumnWithAND()
With ThisWorkbook.Worksheets("AND")
    .Range("B4").AutoFilter Field:=1, Criteria1:=">=" & .Range("E4").Value, Operator:=xlAnd, Criteria2:="<=" & .Range("E5").Value
End With
End Sub

The code is now ready to run.

VBA to AutoFilter with Multiple Criteria with AND on Same Field in Excel

  • Run the macro as we showed you in the above section. The result is shown in the image below.

Result of VBA to AutoFilter with Multiple Criteria with AND on Same Field in Excel

Column B is filtered with the values from 2 to 9, that fulfill both conditions.

VBA Code Explanation

With ThisWorkbook.Worksheets("AND")
    .Range("B4").AutoFilter Field:=1, Criteria1:=">=" & .Range("E4").Value, Operator:=xlAnd, Criteria2:="<=" & .Range("E5").Value
End With

This piece of code,

  • Declare the worksheet name that we will be working with (“AND” is the sheet name for our dataset). You must write the sheet name according to your dataset.
  • Filter Column B starts on Cell B4 according to the multiple criteria stored in Cell E4 and E5 with the xlAnd operator.
  • Leaves the worksheet.

Method 3 – Apply Macro to AutoFilter with OR Operator on Same Column in Excel

Steps:

  • Open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Copy the following code and paste it into the code window.
Sub AutoFilterOnSameColumnWithOR()
With ThisWorkbook.Worksheets("OR")
    .Range("B4").AutoFilter Field:=1, Criteria1:="<" & .Range("E5").Value, Operator:=xlOr, Criteria2:=">" & .Range("E4").Value
End With
End Sub

Your code is now ready to run.

VBA to AutoFilter with Multiple Criteria with OR on Same Field in Excel

  • Run the macro and look at the following image to see the output.

Result of VBA to AutoFilter with Multiple Criteria with OR on Same Field in Excel

Column B is filtered with the values that are greater than or equal to 12 or less than or equal to 7.

VBA Code Explanation

With ThisWorkbook.Worksheets("OR")
    .Range("B4").AutoFilter Field:=1, Criteria1:="<" & .Range("E5").Value, Operator:=xlOr, Criteria2:=">" & .Range("E4").Value
End With

This piece of code,

  • Declare the worksheet name that we will be working with (“OR” is the sheet name for our dataset). You must write the sheet name according to your dataset.
  • Filter Column B starts on Cell B4 according to the multiple criteria stored in Cell E4 and E5 with the xlOr operator.
  • Leaves the worksheet.

Method 4 – Embed VBA to AutoFilter Based on Multiple Text Values on the Same Field

Steps:

  • Open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Copy the following code and paste it into the code window.
Sub AutoFilterOnSameColumnWithMultipleTexts()
Dim iArray As Variant
iArray = Array("Australia", "England")
Range("B4", Range("B" & Rows.Count).End(xlUp)).AutoFilter 1, iArray, xlFilterValues, , 0
End Sub

Your code is now ready to run.

VBA to AutoFilter with Multiple Text Criteria on Same Field in Excel

  • Run the macro. Now, look at the following image to see the result.

Result of VBA to AutoFilter with Multiple Text Criteria on Same Field in Excel

Column B which was filled with so many countries is now filtered only with two country names – Australia and England – that we provided in the code.

VBA Code Explanation

Dim iArray As Variant

Define the variable for the array.

iArray = Array("Australia", "England")

Store the text criteria based on which the filtering will be performed in the defined array.

Range("B4", Range("B" & Rows.Count).End(xlUp)).AutoFilter 1, iArray, xlFilterValues, , 0

Filter Column B starts on Cell B4 according to the multiple text criteria hardcoded in the defined array.


Download Practice Workbook

You can download the free practice Excel workbook from here.


Related Articles

 

 

 

 

 

 

 

Get FREE Advanced Excel Exercises with Solutions!
Sanjida Ahmed
Sanjida Ahmed

Sanjida Ahmed, who graduated from Daffodil International University with a degree in Software Engineering, has worked with SOFTEKO since 2021. She has written over 100 articles on Excel & VBA and, since 2022, has worked as the Project Manager of the Excel Extension Development Project in the Software Development Department. Since starting this software development, she has established an outstanding workflow encompassing a full SDLC. She always tries to create a bridge between her skills and interests in... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo