Excel VBA to Check If AutoFilter is On (4 Easy Ways)

Get FREE Advanced Excel Exercises with Solutions!

Excel’s AutoFilter feature is really efficient in extracting data based on certain conditions. But sometimes due to technical issues, AutoFilter gets hidden or even though there is no filter, Excel shows that there is. In that case, it is quite impossible to understand whether AutoFilter is enabled for the Excel worksheet or not. Implementing VBA is the most effective, quickest, and safest method to run any operation in Excel. In this article, we will show you 4 easy and quick ways to check if AutoFilter is on in Excel with the VBA macro.


Check If AutoFilter is On with Excel VBA: 4 Quick Ways

Following this section, you will learn how to check if AutoFilter is on in Excel with VBA in 4 different ways.

Above is the example dataset that this article will follow to describe the procedures.


1. Embed VBA to Check Whether AutoFilter is Turned On or Off in Excel Worksheet

In this section, you will learn how to check if AutoFilter is on for an Excel worksheet with VBA. The steps to execute this are given below.

Steps:

  • In the beginning, press Alt + F11 on your keyboard or go to the tab Developer -> Visual Basic to open Visual Basic Editor.

  • Next, in the pop-up code window, from the menu bar, click Insert -> Module.

  • Then, copy the following code and paste it into the code window.
Sub AutoFilterCheck()
If ActiveSheet.AutoFilterMode = True Then
MsgBox "Auto Filter is turned on"
Else
MsgBox "Auto Filter is turned off"
End If
End Sub

Your code is now ready to run.

Excel VBA to check if autofilter is on

  • Now, press F5 on your keyboard, or from the menu bar select Run -> Run Sub/UserForm. You can also just click on the Run icon in the sub-menu bar to run the macro.

After successful code execution, look at the following image to check out the result.

Result of excel vba to check if autofilter is on

Now, look at the above image. Even though there is no filter arrow in the dataset, Excel MsgBox is telling us that the AutoFilter is turned on in this sheet.

Implementing VBA is really convenient for these kinds of scenarios where there is uncertainty about any operation; VBA macro helps you extract the correct result even when your eyes can’t see them.

VBA Code Explanation

If ActiveSheet.AutoFilterMode = True Then
MsgBox "Auto Filter is turned on"
Else
MsgBox "Auto Filter is turned off"
End If

This piece of code refers to if the auto filter mode is true for the active sheet, then returns the “Auto Filter is turned on” message in the Excel MsgBox; Otherwise, it throws the “Auto Filter is turned off” message.


2. Debug VBA Code and Get the Count of Total AutoFilter in Active Sheet

This section will show you how you can debug the code and count how many AutoFilters are there in an Excel sheet. After executing the code, it will throw the result in the Immediate window of Excel VBA.

Steps to get that are given below.

Steps:

  • Same way as before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, copy the following code and paste it into the code window.
Sub CountAutoFilters()
Dim iCount As Long
  If ActiveSheet.AutoFilterMode = True Then iCount = 1
  Debug.Print "AutoFilterMode Count: " & iCount
End Sub

Your code is now ready to run.

Debug and count excel vba to check if autofilter is on

  • After that, Run the macro as we showed you in the above section. The result is shown in the image below.

Count for excel vba to check if autofilter is on

As you can see in the image above, in the Immediate window you will get the total count of the enabled AutoFilter in your worksheet.

VBA Code Explanation

Dim iCount As Long

Declaring the variable.

If ActiveSheet.AutoFilterMode = True Then iCount = 1
  Debug.Print "AutoFilterMode Count: " & iCount

This couple of lines in the code means, that if the Autofilter mode is true for the active sheet, then start counting and print the total count.


3. Apply VBA to Check If a Specific Column is Filtered or Not in Excel

Suppose you want to check whether a specific column in your Excel spreadsheet is filtered or not with VBA.

Let’s see the steps on how to do that.

Steps:

  • As shown before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, copy the following code and paste it into the code window.
Sub CheckColumnFilter()
    Dim iSheet As Worksheet
    Set iSheet = ActiveSheet
    If iSheet.AutoFilter.Filters(2).On Then
        MsgBox "Column B is filtered."
    Else
        MsgBox "Column B is not filtered."
    End If
End Sub

Your code is now ready to run.

Excel vba to check if autofilter is on for column

  • Later, Run the macro and look at the following image to see the output.

Now, consider the image above. Even though you can see that there is a filter arrow in column B, the macro is telling us that there is not. So, after successful code execution, we can say that column B is not Auto-filtered.

VBA Code Explanation

Dim iSheet As Worksheet

Declaring the variable for the worksheet.

Set iSheet = ActiveSheet

Store the active sheet in the declared variable.

If iSheet.AutoFilter.Filters(2).On Then
    MsgBox "Column B is filtered."
Else
    MsgBox "Column B is not filtered."
End If

This piece of code refers to whether the auto filter for Column 2 or Column B is true for the active sheet, then returns the “Column B is filtered.” message in the Excel MsgBox; Otherwise, it throws the “Column B is not filtered.” message.


4. VBA Macro to Check for Enabled AutoFilter in Excel Workbook

In this section, you will learn how you can check for enabled or disabled AutoFilter for a sheet in a specific Excel workbook.

The steps to execute this are given below.

Steps:

  • Firstly, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, copy the following code and paste it into the code window.
Sub CheckAutofilterSheet()
Dim z As Double
For z = 1 To ThisWorkbook.Sheets.Count
    If ThisWorkbook.Sheets(z).AutoFilterMode Then
        MsgBox ThisWorkbook.Sheets(z).Name & " has enabled Autofilter"
    End If
Next
End Sub

Your code is now ready to run.

Excel vba to check if autofilter is on in workbook

  • Next, Run the macro. Now, look at the following image to see the result.

Result of Excel vba to check if autofilter is on in workbook

As you can see in the above image, our workbook has a worksheet named “Dataset”. Excel’s MsgBox is showing us that the “Dataset” sheet has AutoFilter on. You can perform this code for all of your spreadsheets in a specific Excel workbook.

VBA Code Explanation

Dim z As Double

Declaring the variable.

For z = 1 To ThisWorkbook.Sheets.Count

Starts looping from the 1st sheet to the total sheet count of the existing workbook.

If ThisWorkbook.Sheets(z).AutoFilterMode Then
    MsgBox ThisWorkbook.Sheets(z).Name & " has enabled Autofilter"
End If
Next

If the first sheet has auto filter mode on, then throw the message in the MsgBox. After that, the code ends the condition and goes to the next sheet to perform the iteration. It continues to do this until it reaches all the sheet count of the current workbook.

Read More: Excel VBA: Remove AutoFilter If It Exists


Download Practice Workbook

You can download the free practice Excel workbook from here.


Conclusion

To conclude, this article showed you 4 easy and quick ways to check if AutoFilter is on in Excel with the VBA macro. I hope this article has been very beneficial to you. Feel free to ask any questions regarding the topic.


Related Articles

What is ExcelDemy?

ExcelDemy - Learn Excel & Get Excel Solutions Center provides online Excel training , Excel consultancy services , free Excel tutorials, free support , and free Excel Templates for Excel professionals and businesses. Feel free to contact us with your Excel problems.
Sanjida Ahmed
Sanjida Ahmed

Sanjida Ahmed, who graduated from Daffodil International University with a degree in Software Engineering, has worked with SOFTEKO since 2021. She has written over 100 articles on Excel & VBA and, since 2022, has worked as the Project Manager of the Excel Extension Development Project in the Software Development Department. Since starting this software development, she has established an outstanding workflow encompassing a full SDLC. She always tries to create a bridge between her skills and interests in... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo