VBA Macro Codes to Filter Data In Excel

Excel offers a lot of built-in filtering options. You can do a lot of basic and advanced filtering using the in-built options.

But with VBA, you can take the filtering functionality to the next level. With a few lines of code, you apply some advanced filtering criteria to your data.

In this tutorial, I will cover ten different examples of how to filter data in Excel using VBA macro codes. These are simple codes that you can easily adapt to your dataset.

So let’s get started!

Example #1: VBA Code to Filter One Column By One Text Criterion

This example shows how to apply Excel VBA code to filter one column by text one criterion.

On “Sheet1” of the example workbook, we have the following dataset that displays different branded electronic items and their categories and prices.

Dataset to filter one column

We want to filter column B by the value “Desktops.”

We use the following steps:

  1. Right-click the “Sheet1” tab and click “View Code” on the context menu.
Click on view code option
  1. Copy and paste the following sub-procedure into the code window that appears:
'Code developed by Steve Scott from https://spreadsheetplanet.com
Sub FilterOneColumn()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")
    ws.Range("B1").AutoFilter Field:=2, Criteria1:="Desktops"
End Sub
  1. Click the “Save” button on the VBE toolbar to save the sub-procedure.
  2. Click “No” on the informational message box and save the workbook as an Excel Macro-Enabled Workbook.
Click "No" on the informational message box
  1. Press the shortcut Alt + F11 to switch to the active worksheet.
  2. Press Alt + F8 to activate the “Macro” dialog box. Select the “Sheet1.FilterOneColumn” macro on the “Macro name” list box and click “Run.”
Run the VBA macro to filter one column in Excel

The code executes and filters column B of the dataset by the value “Desktops,” as shown below:

One column filtered using VBA

Note: I am using the name Sheet1 for the sheet that has the data. If your sheet has a different name, you can change it accordingly in the VBA code

Explanation of the Code

The Excel VBA sub-routine “FilterOneColumn” sets up a filter in the second column (column B) of Sheet1 to show only the rows where the value is “Desktops.”

Here’s a breakdown of the subroutine:

  1. Dim ws As Worksheet – This statement declares a variable named “ws” of type “Worksheet.” This variable is used to reference a worksheet object in the workbook.
  2. Set ws = ThisWorkbook.Sheets(“Sheet1”) – The “Sheet1” object in the current workbook is assigned to the variable “ws.”  
  3. ws.Range(“A1″).AutoFilter Field:=2, Criteria1:=”Desktops” – This is where the filtering takes place:
    • ws.Range(“A1”) – refers to cell A1, the starting cell of the range to which the filter is applied.
    • .AutoFilter – This method activates the AutoFilter feature on the specified range.
    • Field:=2 – This argument specifies the column to filter, column 2 (column B). 
    • Criteria1:=”Desktops” – This argument specifies the filter criteria. In this case, the filter will show only the rows where the values in the second column (column B) match the criteria “Desktops.”

Note: You can change the worksheet name, modify the filter criteria, or adjust the column number to meet your requirements.

Also read: How to Filter as You Type in Excel (With and Without VBA)

Example #2: VBA Code to Filter One Column By Multiple Text Criteria

This example shows how to use Excel VBA code to filter one column by multiple text criteria.

On “Sheet2” of the example workbook, we have the following dataset showcasing different branded electronic items and their categories and prices.

dataset to Filter One Column By Multiple Text Criteria

We want to filter column B by the values “Desktops” and “Laptops.”

We use the following steps:

  1. Right-click the Sheet2 tab and click “View Code” on the shortcut menu.
Click on view code for Sheet2
  1. Copy and paste the below code into the code window that appears:
'Code developed by Steve Scott from https://spreadsheetplanet.com
Sub FilterOneColumnMultipleCriteria()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet2")
    ws.Range("A1").AutoFilter Field:=2, Criteria1:="Desktops", Operator:=xlOr, Criteria2:="Laptops"
End Sub
  1. Press the shortcut Alt + F11 to switch to the current worksheet containing the dataset.
  2. Press the key shortcut Alt + F8 to activate the “Macro” dialog box. Select “Sheet2.FilterOneColumnMultipleCriteria” on the “Macro name” list box and click “Run.”
Run vba macro to Filter One Column By Multiple Text Criteria

The Excel VBA code executes and filters column B by the values “Desktops” and “Laptops,” as shown below:

Explanation of the Code

The VBA code sets up a filter on the second column (column B) of Sheet2 to display rows where the value is either “Desktops” or “Laptops.” 

The breakdown of the code is as follows:

  1. Dim ws As Worksheet – This line declares a variable named “ws” of the data type “Worksheet.”
  2. Set ws = ThisWorkbook.Sheets(“Sheet2”) – This statement assigns “Sheet2” to the “ws” variable.
  3. ws.Range(“A1″).AutoFilter Field:=2, Criteria1:=”Desktops”, Operator:=xlOr, Criteria2:=”Laptops”: This statement applies a filter to the specified range in the worksheet:
    • ws.Range(“A1”) – This specifies the starting cell of the range where the filter will be applied.
    • .AutoFilter – This method activates the AutoFilter functionality for the specified range.
    • Field:=2 – This parameter indicates the column number or field to filter. In this case, it’s the second column (column B) because VBA uses 1-based indexing.
    • Criteria1:=”Desktops” – This sets the first filtering criteria. The rows in the specified column containing the text “Desktops”  will be displayed.
    • Operator:=xlOr – This specifies the “OR” logical operator used between the two criteria meaning that rows meeting either the first or the second criterion will be displayed.
    • Criteria2:=”Laptops” – This sets the second filtering criteria. It specifies that rows in the specified column containing the text “Laptops” will also be displayed.

Note: To customize the code to your requirements, you can change the worksheet’s name and adjust the filter criteria.

Also read: How to Filter Cells with Notes in Excel?

Example #3: VBA Code to Filter Multiple Columns By Multiple Criteria

This example describes utilizing Excel VBA code to filter multiple columns by multiple criteria.

The example workbook’s “Sheet3” has a dataset featuring various branded electronic items, their categories, and prices.

Dataset to Filter Multiple Columns By Multiple Criteria

We want to filter column B by the value “Desktops,” and column C by the value “HP.”

We use the below steps:

  1. Right-click the Sheet3 tab and click “View Code” on the shortcut menu.
Click on view code for Sheet3
  1. Copy and paste the below code into the code window that appears:
'Code developed by Steve Scott from https://spreadsheetplanet.com
Sub FilterMultipleColumns()
    With Worksheets("Sheet3").Range("A1")
        .AutoFilter Field:=2, Criteria1:="Desktops"
        .AutoFilter Field:=3, Criteria1:="HP"
    End With
End Sub
  1. Press the shortcut Alt + F11 to switch to the active worksheet containing the dataset.
  2. Press the key shortcut Alt + F8 to activate the “Macro” dialog box. Select “Sheet2.FilterMultipleColumns” on the “Macro name” list box and click “Run.”
Run vba macro to Filter Multiple Columns By Multiple Criteria

The code executes and filters column B by the value “Desktops” and column C by  “HP.”

Explanation of the Code

The Excel VBA code filters the data in “Sheet3,” starting from cell A1. It first shows only rows where the second column contains “Desktops” and then further narrows down the view to show only rows where the third column contains “HP.” 

Here’s a breakdown of the code:

  1. With Worksheets(“Sheet3”).Range(“A1”) – This statement establishes a “With” block, allowing you to perform a series of actions on a specified range without having to repeat the range reference. In this case, it refers to the worksheet named “Sheet3” and the cell “A1” (the top-left cell of the range you want to filter).
  2. .AutoFilter Field:=2, Criteria1:=”Desktops”– Within the “With” block, this line applies AutoFilter functionality to the range specified earlier. It filters the data in the range’s second column (Field:=2) by “Desktops.” This results in only rows where the value in the second column is “Desktops” being displayed.
  3. .AutoFilter Field:=3, Criteria1:=”HP”: This line filters the third column (Field:=3) by the criteria “HP.” Only rows with “HP” in the third column are displayed.

Note: To customize the code to fit your needs, you can change the worksheet’s name and adjust the filter criteria to meet your requirements.

Also read: How to Count Filtered Rows in Excel?

Example #4: VBA Code to Filter One Column By Multiple Numerical Criteria

This example will show how to filter one column by multiple numerical values using Excel VBA code.

The example workbook’s “Sheet4” has a dataset featuring various branded electronic items, their categories, and prices.

Dataset to Filter One Column By Multiple Numerical Criteria

We want to filter column D to display only those items whose price is greater than $200 and less than $500.

We use the below steps:

  1. Right-click the Sheet4 tab and click “View Code” on the shortcut menu.
Click on view code for Sheet4
  1. Copy and paste the below code into the code window that appears:
'Code developed by Steve Scott from https://spreadsheetplanet.com
Sub FilterOneColumnMultipleNumberCriteria()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet4")
    ws.Range("A1").AutoFilter Field:=4, Criteria1:=">200", Operator:=xlAnd, Criteria2:="<500"
End Sub
  1. Press the shortcut Alt + F11 to switch to the active worksheet containing the dataset.
  2. Press the key shortcut Alt + F8 to activate the “Macro” dialog box. Select “Sheet4.FilterOneColumnMultipleNumberCriteria” on the “Macro name” list box and click “Run.”
Run VBA macro to Filter Multiple Columns By Multiple Criteria

The code executes and filters column D to display only those items whose prices are greater than $200 and less than $500.

Result with Filtered One Column By Multiple Numerical Criteria

Explanation of the Code

The Excel VBA code filters the data in column 4 (column D) of the “Sheet4” worksheet, showing only the rows with values greater than $200 and less than $500. 

Here’s a breakdown of the code:

  1. Dim ws As Worksheet – This line declares a variable named “ws” of the data type “Worksheet.” 
  2. Set ws = ThisWorkbook.Sheets(“Sheet4”) – This statement uses the “Set” keyword to assign the worksheet “Sheet4” object to the variable “ws.” 
  3. ws.Range(“A1″).AutoFilter Field:=4, Criteria1:=”>200″, Operator:=xlAnd, Criteria2:=”<500″ – This line is where the actual filtering takes place. Let’s break down the statement:
    • ws.Range(“A1”) – This specifies the range of cells to be filtered starting with cell A1. 
    • .AutoFilter – This method is used to apply a filter to the specified range.
    • Field:=4 – This parameter specifies column number 4 (column D) to which the filter will be applied. 
    • Criteria1:=”>200″ – This criterion instructs Excel to show only the rows where the values in the specified column (column 4) are greater than 200.
    • Operator:=xlAnd – The “xlAnd” parameter means the first and second criteria must be satisfied.
    • Criteria2:=”<500″ – This second filter criterion instructs Excel to show only the rows where the values in the specified column (column 4) are less than 500.
Also read: How to Paste into Filtered Column Skipping the Hidden Cells?

Example #5: VBA Code to Filter One Column By The Three Top Values

In this example, we will show how to use Excel VBA code to filter the dataset’s top three most expensive items.

The example workbook’s “Sheet5” has a dataset featuring various branded electronic items, their categories, and prices.

Dataset to Filter One Column By The Three Top Values

We want to filter column D to display only the three most expensive items.

We use the following steps:

  1. Right-click the Sheet5 tab and choose “View Code” on the shortcut menu.
Click on view code for Sheet5
  1. Copy and paste the below code into the code window that appears:
'Code developed by Steve Scott from https://spreadsheetplanet.com
Sub FilterTopThreeItems()
    ActiveSheet.Range("A1").AutoFilter Field:=4, Criteria1:="3", Operator:=xlTop10Items
End Sub
  1. Press the shortcut Alt + F11 to switch to the active worksheet containing the dataset.
  2. Press the key shortcut Alt + F8 to activate the “Macro” dialog box. Select “Sheet5.FilterTopThreeItems” on the “Macro name” list box and click “Run.”
Run VBA macro to Filter Multiple Columns By Multiple Criteria

The Excel VBA code executes and filters the three most expensive items on the dataset, as shown below:

VBA code result to Filter One Column By The Three Top Values

Explanation of the Code

The Excel VBA code displays the three most expensive items on the list.

Here’s a breakdown of the code:

  1. ActiveSheet.Range(“A1″).AutoFilter Field:=4, Criteria1:=”3”, Operator:=xlTop10Items – This statement applies a filter to the range starting with cell A1 on the active worksheet. Here’s what each part of this line does:
    • ActiveSheet – Refers to the active worksheet.
    • .Range(“A1”) – Specifies the range starting with cell A1 to which the filter is to be applied.
    • .AutoFilter – Activates the AutoFilter functionality for the specified range.
    • Field:=4 – Indicates column 4 (column D) to which the filter will be applied.
    • Criteria1:=”3″ – Sets the filter criteria to show only the three highest-valued items on the dataset.
    • Operator:=xlTop10Items – Highest-valued items displayed (number of items specified in Criteria1)

Note: You can modify the code by changing the target field and criteria to suit your needs.

Also read: 100 Useful Excel VBA Macro Codes Examples

Example #6: VBA Code to Filter By Top 25% Highest-Valued Items

In this example, we describe how to apply Excel VBA code in filtering the top 25 percent of the highest-valued items of a dataset.

On “Sheet6” of the example workbook, a dataset includes different electronic items from various brands, their respective categories, and prices.

Dataset to Filter By Top 25% Highest-Valued Items

We want to filter column D to display only 25 percent of the highest-valued items on the dataset.

We use the following steps:

  1. Right-click the Sheet6 tab and choose “View Code” on the shortcut menu.
Click on view code for Sheet6
  1. Copy and paste the below code into the code window that appears:
'Code developed by Steve Scott from https://spreadsheetplanet.com
Sub FilterTop25Percent()
    ActiveSheet.Range("A1").AutoFilter Field:=4, Criteria1:="25", Operator:=xlTop10Percent
End Sub
  1. Press the shortcut Alt + F11 to switch to the active worksheet containing the dataset.
  2. Press the key shortcut Alt + F8 to activate the “Macro” dialog box. Select “Sheet6.FilterTop25Percent” on the “Macro name” list box and click “Run.”
Run VBA macro to Filter By Top 25% Highest-Valued Items

The sub-routine executes and filters the top 25 percent of the highest-valued items on the dataset, as shown below:

Data filtered by VBA

Note: The dataset has nine items, meaning 25 percent of 9 is approximately 2.

Explanation of the Code

The Excel VBA code filters the top 25 percent of the highest-valued items on the dataset. 

Here’s a breakdown of the code:

  1. ActiveSheet.Range(“A1”) – This specifies the range on the active worksheet starting with cell A1 to which the filter is applied. 
  2. .AutoFilter Field:=4, Criteria1:=”25″, Operator:=xlTop10Percent – This is the line where the AutoFilter operation is defined:
    • Field:=4 – This specifies column 4 (column D) to which the filter criteria are applied.   
    • Criteria1:=”25″ – This sets the filtering criterion, which is 25 percent of the highest-valued items on the dataset.
    • Operator:=xlTop10Percent – Highest-valued items displayed (percentage specified in Criteria1)

Note: You can customize the code by changing the column number and percentage criteria to meet your requirements.

Example #7: Use Wildcard Characters in Excel VBA Code to Filter Data in Excel 

This example shows how to use the asterisk (*) wildcard character in Excel VBA code to filter data in Excel.

On “Sheet7” of the example workbook, a dataset includes different electronic items from various brands, their respective categories, and prices.

dataset to filter using wildcard in VBA

We want to filter column D to display only those items that contain the word “Dell.”

We use the following steps:

  1. Right-click the Sheet7 tab and choose “View Code” on the shortcut menu.
  2. Copy and paste the below code into the code window that appears:
'Code developed by Steve Scott from https://spreadsheetplanet.com
Sub FilterByWildcard()
    ActiveSheet.Range("A1").AutoFilter Field:=1, Criteria1:="*Dell*"
End Sub
  1. Press the shortcut Alt + F11 to switch to the active worksheet containing the dataset.
  2. Press the key shortcut Alt + F8 to activate the “Macro” dialog box. Select “Sheet7.FilterByWildcard” on the “Macro name” list box and click “Run.”
Run VBA macro to Filter using wildcard character in VBA

The code executes and displays only those items containing the word “Dell,” as shown below:

Dataset filtered using VBA code

Explanation of the Code

This Excel VBA code filters and displays rows in the first column that contain variations of the word “Dell” using the asterisk (*) wildcard character.

Here’s the breakdown of the code:

  1. ActiveSheet.Range(“A1″).AutoFilter Field:=1, Criteria1:=”*Dell*”: This is the main line of code that performs the filtering operation:
    • ActiveSheet: This refers to the currently active worksheet in Excel.
    • Range(“A1”): This specifies the starting cell of the range to be filtered. 
    • .AutoFilter: Autofilter method filters data based on specific criteria.
    • Field:=1: This parameter specifies column 1 (column A) to be filtered.
    • Criteria1:=”*Dell*”: This is the filtering criteria. The asterisks (*) are wildcards, and “Dell” is the text you want to filter for. This filter will show all rows where the data in the first column contains the text “Dell” anywhere within it.

Note: To tailor the code to your needs, modify the worksheet name and criteria wildcard characters. For example, if you want to filter data where the text in cells starts from a specific word (say Dell), you can use Dell* and to filter data where the text in cells ends with a specific word, you can use *Dell.

Example #8: VBA Code to Filter Data By Cell Color in Excel

This example shows utilizing Excel VBA code to filter data by cell color.

On “Sheet8” of our example workbook, a dataset includes different electronic items from various brands, their respective categories, and prices. Some of the cells in the price column are red.

dataset with some cells that are colored

We want to apply a filter on column D to display only red cells.

We use the following steps:

  1. Right-click the Sheet8 tab and choose “View Code” on the shortcut menu.
  2. Copy and paste the below code into the code window that appears:
'Code developed by Steve Scott from https://spreadsheetplanet.com
Sub FilterByCellColor()
    ActiveSheet.Range("A1").AutoFilter Field:=4, Criteria1:=RGB(255, 0, 0), Operator:=xlFilterCellColor
End Sub
  1. Press the shortcut Alt + F11 to switch to the active worksheet containing the dataset.
  2. Press the key shortcut Alt + F8 to activate the “Macro” dialog box. Select “Sheet8.FilterByCellColor” on the “Macro name” list box and click “Run.”
Run VBA macro to Filter based on cell color

The Excel VBA code executes and displays only red cells in column D, as shown below:

Dataset filtered by vba code based on cell color

Explanation of the Code

The Excel VBA code applies an AutoFilter to the range starting from cell A1 and filters the data to show only the red cells in column D. 

Here’s the breakdown of the code:

  1. ActiveSheet.Range(“A1”): This line specifies cell A1, the starting cell of the range to be filtered.
  2. .AutoFilter field:=4, Criteria1:=RGB(255, 0, 0), Operator:=xlFilterCellColor: This line applies the AutoFilter feature to the specified range:
    • field:=4 – This argument specifies the column to be filtered, the fourth column (column D),  since Excel uses a 1-based index for columns. 
    • Criteria1:=RGB(255, 0, 0) – This argument defines the color criteria for the filter. The RGB() function is used to specify the color. In this case, it’s RGB(255, 0, 0), representing red, meaning the code will filter the cells that are colored red in the specified column.
    • Operator:=xlFilterCellColor – This argument specifies the type of filter operation to perform. xlFilterCellColor indicates that you want to filter cells based on their color.

Note: You can modify the column number and color criteria to customize the filtering behavior according to your needs.

Example #9: VBA Code to Filter Data By a Particular Date

This example shows how to use Excel VBA code to filter data by a particular date.

 On “Sheet9” of our example workbook, a dataset includes different electronic items from various brands, their respective categories, prices, and purchase dates. 

Dataset to Filter Data By a Particular Date

We want to filter column E to show only those items bought on July 17, 2022.

We use the following steps:

  1. Right-click the Sheet9 tab and choose “View Code” on the shortcut menu.
  2. Copy and paste the below code into the code window that appears:
'Code developed by Steve Scott from https://spreadsheetplanet.com
Sub FilterByParticularDate()
    Dim ws As Worksheet
    Dim targetDate As Date
    targetDate = DateSerial(2022, 7, 17)
    Set ws = ThisWorkbook.Sheets("Sheet9")
    ws.Range("A1").AutoFilter Field:=5, Criteria1:=targetDate

End Sub
  1. Press the shortcut Alt + F11 to switch to the active worksheet containing the dataset.
  2. Press the key shortcut Alt + F8 to activate the “Macro” dialog box. Select “Sheet9.FilterByParticularDate” on the “Macro name” list box and click “Run.”
Run macro to Filter Data By a Particular Date using VBA

The Excel VBA code runs and displays only those items bought on July 17, 2022:

Result when dataset is filtered by date

Explanation of the Code

The Excel VBA code defines a macro that filters data in the “Sheet9” worksheet based on July 17, 2022. It applies an AutoFilter to column E, filtering for rows where the date matches the specified date.

Let’s break down the code:

  1. Dim ws As Worksheet – This line declares a variable named “ws” of type “Worksheet,” which is used to reference a worksheet.
  2. Dim targetDate As Date – This statement declares a variable named “targetDate” of type Date, which holds the specific date you want to filter by.
  3. targetDate = DateSerial(2022, 7, 17) – This line assigns a value to the “targetDate” variable using the ‘DateSerial’ function. The ‘DateSerial’ function constructs a Date value using the specified year (2022), month (7 for July), and day (17).
  4. Set ws = ThisWorkbook.Sheets(“Sheet9”) – This line uses the “Set” keyword to assign the “Sheet9” worksheet to the “ws” variable.
  5. ws.Range(“A1”).AutoFilter Field:=5, Criteria1:=targetDate – This statement applies an AutoFilter to the range starting with cell A1 of the worksheet referred to by the “ws” variable. The AutoFilter is set to filter data in the 5th column (Field:=5) based on the criteria defined by the “targetDate” variable.

Note: You can customize the code for your requirements by modifying the name of the worksheet, the range, and the date.

Example #10: VBA Code to Filter Data By a Date Range

This example shows how to use Excel VBA code to filter data by a date range.

 On “Sheet10” of our example workbook, there is a dataset containing different electronic items from various brands, their respective categories, prices, and purchase dates. 

Dataset to filter by a date range

We want to filter column E to display items purchased from January 1, 2021, to December 31, 2022, including both dates.

We use the following steps:

  1. Right-click the Sheet10 tab and choose “View Code” on the shortcut menu.
  2. Copy and paste the following code into the code window that appears:
'Code developed by Steve Scott from https://spreadsheetplanet.com
Sub FilterByDateRange()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet10")
    ws.Range("A1").AutoFilter Field:=5, Criteria1:=">=01/01/2021", Operator:=xlAnd, Criteria2:="<=12/31/2022"
End Sub
  1. Press the shortcut Alt + F11 to switch to the active worksheet containing the dataset.
  2. Press the key shortcut Alt + F8 to activate the “Macro” dialog box. Select “Sheet10.FilterByDateRange” on the “Macro name” list box and click “Run.”
Run VBA code to Filter Data By a Date Range

The Excel VBA code executes and displays only those items bought from January 1, 2021, to December 31, 2022, including both dates:

Result when dataset is filtered by date range

Explanation of the Code

Once the Excel VBA code runs, it implements an AutoFilter on column 5 (column E) of the designated worksheet.

The filter only shows rows with dates falling between January 1, 2021, and December 31, 2022, including both dates. 

Let’s break down the code step by step:

  1. Dim ws As Worksheet – This line declares a variable named “ws” of type “Worksheet.” This variable is used to store a reference to a worksheet object.
  2. Set ws = ThisWorkbook.Sheets(“Sheet10”) – The “Set” keyword is used to assign the “Sheet10” worksheet to the “ws” variable. 
  3. ws.Range(“A1″).AutoFilter Field:=5, Criteria1:=”>=01/01/2021″, Operator:=xlAnd, Criteria2:=”<=12/31/2022″ – This statement filters data within the specified range:
    • ws.Range(“A1”) – This part of the code specifies cell A1, the starting cell of the range to which the AutoFilter is applied.
    • .AutoFilter – This method activates the AutoFilter functionality for the specified range.
    • Field:=5 – This parameter specifies the column number or field that needs filtering. In this instance, it refers to the second column (column B) because Excel VBA uses 1-based indexing.
    • Criteria1:=”>=01/01/2021″ – This sets the first filter criteria, which means the filter will only include dates greater than or equal to January 1, 2021.
    • Operator:=xlAnd – This parameter specifies the “xlAnd” logical operator used to combine the two filter criteria, meaning both criteria should be satisfied simultaneously.
    • Criteria2:=”<=12/31/2022″ – This sets the second filter criteria, which means the filter will only include dates less than or equal to December 31, 2022.

Note: To tailor the code to your requirements, modify the worksheet name, range, criteria, and logical operator.

Example 11 – Filter Cells with Notes using VBA

If you have notes in your dataset and you want to only filter those cells that have these notes (previously called comments), you can use the VBA macro code below:

'Code developed by Steve Scott from https://spreadsheetplanet.com
Sub FilterCellsWithNotes()
    Dim ws As Worksheet
    Dim cell As Range
    Dim targetRange As Range

    'Set the worksheet object
    Set ws = ThisWorkbook.Sheets("Sheet1")
    
    'Set the target range
    Set targetRange = ws.Range("A1:A10")
    
    'Loop through each cell in the range
    For Each cell In targetRange
        If cell.Comment Is Nothing Then
            ' Hide the row if the cell has no comment
            cell.EntireRow.Hidden = True
        Else
            'Unhide the row if the cell has a comment
            cell.EntireRow.Hidden = False
        End If
    Next cell
End Sub

In the above example, I have my data in ‘Sheet1’ and the range is A1:A10. You can adjust the sheet name and the range based on your dataset.

Advantages of Using VBA to Filter Data in Excel

Below I have listed some advantages of learning how to filter your data using VBA:

  • You can implement more complex logic: Although Excel’s filter options are helpful for basic filtering tasks, Excel VBA code allows you to execute more intricate filtering logic. With Excel VBA code, you can use multiple criteria, establish multiple conditional filters, and apply complex filtering conditions that may not be possible through the standard user interface.
  • You can reuse the code: After creating an Excel VBA script to filter data, you can use it repeatedly for similar filtering tasks guaranteeing a consistent filtering process that isn’t affected by user input.
  • Efficiency: Performing filtering tasks manually through the UI can take much time, especially when dealing with large datasets. However, these tasks can be executed much faster with Excel VBA code resulting in significant time savings, particularly if the tasks need to be repeated frequently.

This tutorial presented ten examples of how to use Excel VBA code to filter data in Excel. We hope you found the tutorial helpful.

Other Excel articles you may also like:

I am a huge fan of Microsoft Excel and love sharing my knowledge through articles and tutorials. I work as a business analyst and use Microsoft Excel extensively in my daily tasks. My aim is to help you unleash the full potential of Excel and become a data-slaying wizard yourself.

Leave a Comment