VBA Filter function is very useful for filtering the records to suit our requirement. We use VBA Filter 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 to create the clean data set. You need to retrieve few records from the large number of records based on some criteria.

VBA Filter

In this topic:

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 functions to filter the data using VBA, Filter function in and Autofilter method in Excel VBA. You can use one of this based on your requirement. If you want to filter array elements, you can use VBA Filter function. And you can use Autofilter method to filter the data in Excel Worksheets.

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.

VBA Filter Function Array

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

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.

Here is the syntax for VBA AutoFilter method

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 Code on AutoFilter Method in Excel VBA

Here is the simple macro to filter the data in range A1 to H100 of active worksheet. It will match cells of column 2 and display the records if cells of column 2 contains “a”.
VBA Autofilter Method

Sub sbAT_VBA_AutoFilter_Method()

ActiveSheet.Range("$A$1:$H$100").AutoFilter Field:=2, Criteria1:="=*a*"

End Sub

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Excel VBA Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ Project Management Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates
MS Word Pack
25+ Word PM Templates
Ultimate Project Management Template
Ultimate Resource Management Template
Project Portfolio Management Templates
Manage Your Projects

120+ Project Management Templates

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Share Post
By Last Updated: July 15, 2022Categories: VBA Filter

One Comment

  1. Mike August 22, 2018 at 12:04 PM - Reply

    Very useful, Thanks for providing the vba code on autofilter.

Leave A Comment