VBA AutoFilter Method is very useful for filtering the records to suit our requirement. We use VBA AutoFilter function to filter the records with verity of criteria to get the limited number of records. Sometimes you need to refine and exclude the unwanted rows in Excel 2007, 2010, 2013, 2016 and latest Microsoft 365 to create the filtered data set. You can retrieve specific records from the large number of records based on some criteria.
In this topic:
- Filtering the Data using VBA
- VBA AutoFilter Method
- Syntax of VBA AutoFilter
- Examples of VBA AutoFilter
- Download Example File – VBA AutoFilter Examples
- VBA Filter Function
- Examples of VBA Filter Function
We will see the different use cases of Filtering the Data using VBA in this topic. We explain each topic with suitable data, VBA code and the filtered records (its outputs) with the screenshots.
VBA AutoFilter – Filtering the Data using VBA:
We have two methods to filter the data using VBA, AutoFilter Method and Filter Function in Excel VBA. You can use one of this based on your requirement. You can use Autofilter method to filter the data in Excel Range or Worksheets. If you want to filter array elements, you can use VBA Filter function.
VBA AutoFilter Method
Helps to filter the data in Excel Range based on specific condition. You can use this function in any MS Excel to filter rows by based on required criteria.
Syntax of VBA AutoFilter
Here is the syntax of the AutoFilter method of Excel VBA. It has 4 arguments, two are required arguments and other two are optional arguments.
Range.AutoFilter( Field , Criteria1 , Operator , Criteria2 , VisibleDropDown )
- Field: offset number of the field of the target range.
- Criteria1: First criteria to filter the data
- Operator: Logical Operator to apply between Criteria 1 and 2
List of available operators: xlAnd, xlOr, xlBottom10Items, xlTop10Items, xlBottom10Percent, xlTop10Percent, xlFilterCellColor, xlFilterDynamic, xlFilterFontColor, xlFilterIcon, xlFilterValues
- Criteria2: First criteria to filter the data.
- VisibleDropDown: True to display the AutoFilter drop-down arrow for the filtered field. False to hide the drop-down arrow, default value is True.
Example Codes on AutoFilter Method in Excel VBA
Here are the simple Examples shows you how to filter data using VBA AutoFilter Method.
Example 1: VBA Filter Wildcard
Here is the simple macro to filter the data in range A1 to H100 of active worksheet based on the wildcard match string. It will match cells of column 2 and display the records if cells of column 2 ends with ‘UAT’. Here ‘*UAT*’ is the wildcard match string which filters the Field 2based on the criteria ‘Ending with UAT’.
Sub sbAT_VBA_AutoFilter_Method() ActiveSheet.Range("$A$1:$H$100").AutoFilter Field:=2, Criteria1:="*UAT" End Sub
Example 2: VBA AutoFilter Multiple Criteria
The following example will help you to understand how to Filter the Data based on multiple Criteria. We set multiple VBA AutoFilter Criteria to filter the data based on multiple Columns.
Sub sbAT_VBA_Macro_To_FilterMultipleColumn() ' VBA Code to filter records of Columns A to F based on the data item in Multiple Columns (Column C and D) With ActiveSheet.Range("$C$5:$H$105") .AutoFilter Field:=3, Criteria1:=Range("E3") .AutoFilter Field:=4, Criteria1:=Range("F3") End With End Sub
Example 3: Clearing VBA AutoFilter
You can simply toggle the AutoFilter Method to Clear all the Filters set to a Range. Please see the below code to learn how to clear the Filter using VBA.
Sub sbAT_ClearAutoFilter() ActiveSheet.Range("$C$5:$H$105").AutoFilter End Sub
VBA AutoFilter- Example Workbook
Here is the Workbook with VBA AutoFilter Method Examples explained with sample data. Example Codes in the Attached file will help you to explore the code and understand the AutoFilter Method with real-time example codes.
VBA Filter Function:
Helps to filter the array elements based on match string. You can use this function in any MS Office application to filter an array by matching a string.
Syntax of VBA Filter
Here is the syntax of the Filter function in VBA. It has 4 arguments, two are required arguments and other two are optional arguments.
Here is the syntax for VBA Filter Function
Filter( SourceArray, Match, [Include], [Compare] )
SourceArray is your main array contains the list of items.
Match is the string which you want to match with the array items.
Include is optional parameter and a Boolean True or False. True to includes the match items, and False do not include the match string.
Compare is an optional parameter and it is to specify the comparison type:
- vbBinaryCompare: To performs a binary comparison, this is the default value.
- vbTextCompare : To performs a text comparison
- vbDatabaseCompareTo performs a database comparison
Example Code on Filter Function in VBA:
Here is the Example code to filter the data using VBA Filter Function. This will filter an array matching with a string.
Sub sbAT_VBA_Filter_Function() 'Declare an array variable to store the collection of items Dim myStringsArray As Variant 'Store few names in myStringsArray for testing purpose myStringsArray = Array("Ravi", "Mike", "Allen", "Tom", "Jenny", "James") 'Create another function to store the filtered data Dim myStringsArray_Filtered As Variant 'Let's Filter the contains "a" myStringsArray_Filtered = Filter(myStringsArray, "a") 'Let's Filter the NOT contains "a" myStringsArray_FilteredFalse = Filter(myStringsArray, "a", False) End Sub
Very useful, Thanks for providing the vba code on autofilter.