REAL-TIME

VBA Projects

Full Access with Source Code

  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

120+ PROFESSIONAL

Project Management Templates

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Effortlessly Manage Your Projects

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

Share Post

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:

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.

VBA AutoFilter Syntax

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’.

VBA Autofilter Method

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.

VBA AutoFilter Example

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 AutoFilter Method Examples

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

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.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM 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
Categories: VBA, VBA FilterLast Updated: July 20, 2023

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