Filter Different Columns by Multiple Criteria in Excel VBA – 3 Methods

This is the sample dataset.

excel vba filter multiple criteria different column

Method 1 – Filtering Different Columns by Multiple Criteria Using the VBA With Statement

To Filter TVs costing more than $1500:

Steps:

  • Open the Developer Tab and select Visual Basic.

  • In Microsoft Visual Basic for Application, select Insert >> Module

excel vba filter multiple criteria different column

  • Enter the following code in the VBA Module.
Sub MultipleCriteria()
With Range("B4:E4")
.AutoFilter Field:=2, Criteria1:="TV"
.AutoFilter Field:=3, Criteria1:=">=1500"
End With
End Sub

The Macro name is defined as MultipleCriteria. Criteria are set using the VBA Range method. As TV is in column 2 (Products), the AutoFilter Field is set as 2 and the Autofilter Field is set as 3: the Expenses column. The code will return the TVs bought for a value more than or equal to $1500.

  • Go back to the sheet and run the Macro (MultipleCriteria).

excel vba filter multiple criteria different column

This is the output.

excel vba filter multiple criteria different column

 

Read More: Filter Multiple Criteria in Excel with VBA (Both AND and OR Types)


Method 2 – Using Excel VBA and the AND Operator to Filter Different Columns by Multiple Criteria

Steps:

  • Enter the following code in the VBA Module. (To see how to open a VBA Module, see Method 1)
Sub MultipleCriteriaAndFilter()
Worksheets("xland_filter").Range("B4:E13").AutoFilter Field:=4, _
Criteria1:=">2", Operator:=xlAnd, Criteria2:="<=5"
End Sub

The Macro name is defined as MultipleCriteriaAndFilter. B4:E13 is defined using the Range Method. There are 2 different criteria which are set using Criteria Property of VBA. 

  • Go back to the sheet and run the Macro (MultipleCriteriaAndFilter).

excel vba filter multiple criteria different column

This is the output.

 

Read More: How to Filter Based on Cell Value Using Excel VBA


Method 3 – Using Excel VBA and the OR Operator to Filter Different Columns by Multiple Criteria

To Filter products with a price less than 1600 dollars or more than or equal to 2100:

Steps:

  • Enter the following code in the VBA Module. (To see how to open a VBA Module, see Method 1)

 

Sub MultipleCriteriaOrFilter()
Worksheets("xlor_filter").Range("B4").AutoFilter Field:=3, _
Criteria1:="<1600", Operator:=xlOr, Criteria2:=">=2100"
End Sub

The Macro name is defined as MultipleCriteriaOrFilter. Range B4 is defined using the Range Method. There are 2 different criteria which are set using the Criteria Property of VBA. Information about Expense is set in Criteria1.

  • Go back to the sheet and run the Macro (MultipleCriteriaOrFilter).

excel vba filter multiple criteria different column

This is the output.

 

Read More: Excel VBA: How to Filter with Multiple Criteria in Array


Practice Section

Practice here.

excel vba filter multiple criteria different column


Download Practice Workbook

 


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Meraz Al Nahian
Meraz Al Nahian

Md. Meraz Al Nahian has worked with the ExcelDemy project for over 1.5 years. He wrote 140+ articles for ExcelDemy. He also solved a lot of user problems and worked on dashboards. He is interested in data analysis, advanced Excel, statistics, and dashboards. He also likes to explore various Excel and VBA applications. He completed his graduation in Electrical & Electronic Engineering from Bangladesh University of Engineering & Technology (BUET). He enjoys exploring Excel-related features to gain efficiency... Read Full Bio

2 Comments
  1. Thanks for the tutorial, but your “or” example doesn’t work. Your example is that you want to return any order where a customer purchased a television OR the customer spent less than or equal to $2000 on any item. Instead, your code returns all TV purchases regardless of amount. You are applying the Expense filter to Field 2 instead of Field 3. Since there are no rows where Product = $2000 or less, that filter is ignored and all TVs are returned. What you want is code where the result is Adam, Alan, Bryan, Jon, Lambert, James (all of whom purchased items for $2k or less) AND Shawn who bought a TV at any price.

    • Hello Mr. Gurry, thank you so much for the feedback. I have updated the article according to your suggestions. What I did before is that I tried to use the xlOr operator for two different AutoFilter Field. But this can be done by With Statement and I showed the process in Method 1. Here I changed the conditions too. Please check again if there’s anything you want to add.

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo