A lot of Excel functionalities are also available to be used in VBA – and the Autofilter method is one such functionality. If you have a dataset and you want to filter it using a criterion, you can easily do it using the Filter option in the Data ribbon. And if you want a more advanced version of it, there is an advanced filter in Excel as well. Then Why Even Use the AutoFilter in VBA? If you just need to filter data and do some basic stuff, I would recommend stick to the inbuilt Filter functionality that Excel interface offers. You should use VBA Autofilter when you want to filter the data as a part of your automation (or if it helps you save time by making it faster to filter the data). For example, suppose you want to quickly filter the data based on a drop-down selection, and then copy this filtered data into a new worksheet. While this can be done using the inbuilt filter functionality along with some copy-paste, it can take you a lot of time to do this manually. In such a scenario, using VBA Autofilter can speed things up and save time. Note: I will cover this example (on filtering data based on a drop-down selection and copying into a new sheet) later in this tutorial.

Excel VBA Autofilter Syntax

Expression: This is the range on which you want to apply the auto filter. Field: [Optional argument] This is the column number that you want to filter. This is counted from the left in the dataset. So if you want to filter data based on the second column, this value would be 2. Criteria1: [Optional argument] This is the criteria based on which you want to filter the dataset. Operator: [Optional argument] In case you’re using criteria 2 as well, you can combine these two criteria based on the Operator. The following operators are available for use: xlAnd, xlOr, xlBottom10Items, xlTop10Items, xlBottom10Percent, xlTop10Percent, xlFilterCellColor, xlFilterDynamic, xlFilterFontColor, xlFilterIcon, xlFilterValues Criteria2: [Optional argument] This is the second criteria on which you can filter the dataset. VisibleDropDown: [Optional argument] You can specify whether you want the filter drop-down icon to appear in the filtered columns or not. This argument can be TRUE or FALSE.

Apart from Expression, all the other arguments are optional. In case you don’t use any argument, it would simply apply or remove the filter icons to the columns. The above code would simply apply the Autofilter method to the columns (or if it’s already applied, it will remove it). This simply means that if you can not see the filter icons in the column headers, you will start seeing it when this above code is executed, and if you can see it, then it will be removed. In case you have any filtered data, it will remove the filters and show you the full dataset. Now let’s see some examples of using Excel VBA Autofilter that will make it’s usage clear.

Example: Filtering Data based on a Text condition

Suppose you have a dataset as shown below and you want to filter it based on the ‘Item’ column.

The below code would filter all the rows where the item is ‘Printer’. The above code refers to Sheet1 and within it, it refers to A1 (which is a cell in the dataset). Note that here we have used Field:=2, as the item column is the second column in our dataset from the left. But before I show you those, let me first cover a few examples to show you what all the AutoFilter method can do. You’re right! If this is all you want to do, better used the inbuilt Filter functionality. But as you read the remaining tutorial, you’ll see that this can be combined with some extra code to create powerful automation. Click here to download the example file and follow along.

Example: Multiple Criteria (AND/OR) in the Same Column

Suppose I have the same dataset, and this time I want to filter all the records where the item is either ‘Printer’ or ‘Projector’.

The below code would do this: Note that here I have used the xlOR operator. This tells VBA to use both the criteria and filter the data if any of the two criteria are met. Similarly, you can also use the AND criteria. For example, if you want to filter all the records where the quantity is more than 10 but less than 20, you can use the below code:

Example: Multiple Criteria With Different Columns

Suppose you have the following dataset.

With Autofilter, you can filter multiple columns at the same time. For example, if you want to filter all the records where the item is ‘Printer’ and the Sales Rep is ‘Mark’, you can use the below code:

Example: Filter Top 10 Records Using the AutoFilter Method

Suppose you have the below dataset.

Below is the code that will give you the top 10 records (based on the quantity column): In the above code, I have used ActiveSheet. You can use the sheet name if you want. Note that in this example, if you want to get the top 5 items, just change the number in Criteria1:=”10″ from 10 to 5. So for top 5 items, the code would be: It may look weird, but no matter how many top items you want, the Operator value always remains xlTop10Items. Similarly, the below code would give you the bottom 10 items: And if you want the bottom 5 items, change the number in Criteria1:=”10″ from 10 to 5.

Example: Filter Top 10 Percent Using the AutoFilter Method

Suppose you have the same data set (as used in the previous examples). Below is the code that will give you the top 10 percent records (based on the quantity column): In our dataset, since we have 20 records, it will return the top 2 records (which is 10% of the total records).

Example: Using Wildcard Characters in Autofilter

Suppose you have a dataset as shown below:

If you want to filter all the rows where the item name contains the word ‘Board’, you can use the below code: In the above code, I have used the wildcard character * (asterisk) before and after the word ‘Board’ (which is the criteria). An asterisk can represent any number of characters. So this would filter any item that has the word ‘board’ in it.

Example: Copy Filtered Rows into a New Sheet

If you want to not only filter the records based on criteria but also copy the filtered rows, you can use the below macro. It copies the filtered rows, adds a new worksheet,  and then pastes these copied rows into the new sheet. The above code would check if there are any filtered rows in Sheet1 or not. If there are no filtered rows, it will show a message box stating that. And if there are filtered rows, it will copy those, insert a new worksheet, and paste these rows on that newly inserted worksheet.

Example: Filter Data based on a Cell Value

Using Autofilter in VBA along with a drop-down list, you can create a functionality where as soon as you select an item from the drop-down, all the records for that item are filtered. Something as shown below: Click here to download the example file and follow along. This type of construct can be useful when you want to quickly filter data and then use it further in your work. Below is the code that will do this: This is a worksheet event code, which gets executed only when there is a change in the worksheet and the target cell is B2 (where we have the drop-down). Also, an If Then Else condition is used to check if the user has selected ‘All’ from the drop down. If All is selected, the entire data set is shown. This code is NOT placed in a module. Instead, it needs to be placed in the backend of the worksheet that has this data. Here are the steps to put this code in the worksheet code window: Now when you use the drop-down list, it will automatically filter the data. This is a worksheet event code, which gets executed only when there is a change in the worksheet and the target cell is B2 (where we have the drop-down). Also, an If Then Else condition is used to check if the user has selected ‘All’ from the drop down. If All is selected, the entire data set is shown.

Turn Excel AutoFilter ON/OFF using VBA

When applying Autofilter to a range of cells, there may already be some filters in place. You can use the below code turn off any pre-applied auto filters: This code checks the entire sheets and removes any filters that have been applied. If you don’t want to turn off filters from the entire sheet but only from a specific dataset, use the below code: The above code checks whether there are already filters in place or not. If filters are already applied, it removes it, else it does nothing. Similarly, if you want to turn on AutoFilter, use the below code:

Check if AutoFilter is Already Applied

If you have a sheet with multiple datasets and you want to make sure you know that there are no filters already in place, you can use the below code. This code uses a message box function that displays a message ‘There are Filters already in place’ when it finds filters on the sheet, else it shows ‘There are no filters’.

Show All Data

If you have filters applied to the dataset and you want to show all the data, use the below code: The above code checks whether the FilterMode is TRUE or FALSE. If it’s true, it means a filter has been applied and it uses the ShowAllData method to show all the data. Note that this does not remove the filters. The filter icons are still available to be used.

Using AutoFilter on Protected Sheets

By default, when you protect a sheet, the filters won’t work. In case you already have filters in place, you can enable AutoFilter to make sure it works even on protected sheets. To do this, check the Use Autofilter option while protecting the sheet.

While this works when you already have filters in place, in case you try to add Autofilters using a VBA code, it won’t work. Since the sheet is protected, it wouldn’t allow any macro to run and make changes to the Autofilter. So you need to use a code to protect the worksheet and make sure auto filters are enabled in it. This can be useful when you have created a dynamic filter (something I covered in the example –  ‘Filter Data based on a Cell Value’). Below is the code that will protect the sheet, but at the same time, allow you to use Filters as well as VBA macros in it. This code needs to be placed in ThisWorkbook code window. Here are the steps to put the code in ThisWorkbook code window: As soon as you open the workbook and enable macros, it will run the macro automatically and protect Sheet1. However, before doing that, it will specify ‘EnableAutoFilter = True’, which means that the filters would work in the protected sheet as well. Also, it sets the ‘UserInterfaceOnly’ argument to ‘True’. This means that while the worksheet is protected, the VBA macros code would continue to work.

Excel VBA Loops. Filter Cells with Bold Font Formatting. Recording a Macro. Sort Data Using VBA. Sort Worksheet Tabs in Excel.

With Worksheets(“Sheet1”).Range(“A1″) .AutoFilter field:=2, Criteria1:=”Printer” .AutoFilter field:=3, Criteria1:=”Mark” .AutoFilter field:=5, Criteria1:=”Fax” .AutoFilter field:=9, Criteria1:=<15 End With Also, is it possible to have several .AutoFilters on the same field without using Criteria2, i.e. With Worksheets(“Sheet1”).Range(“A1”) .AutoFilter field:=2, Criteria1:=“Printer” .AutoFilter field:=2, Criteria1:=”Fax” .AutoFilter field:=2, Criteria1:=”Copier” .AutoFilter field:=9, Criteria1:=<15 End With I have a case where I want to filter out text that matches any of several long boilerplate strings. Also, can Criteria1 (or 2) use a UDF? Worksheets(“Sheet1”).Range(“A1″).AutoFilter Field:=2, Criteria1:=”Opening Balance” Is there a way to instruct VBA to select all items but not (different than) something?