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

Excel’s AutoFilter feature is really efficient to extract 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.


Download Workbook

You can download the free practice Excel workbook from here.


4 Quick Ways with VBA to Check If AutoFilter is On in Excel

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 that 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 small 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 to 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.

Read More: Excel VBA: Remove AutoFilter If It Exists (7 Ways)


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 auto filter mode is true for the active sheet, then start counting and print the total count.

Read More: VBA to AutoFilter with Multiple Criteria on Same Field in Excel (4 Methods)


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

VBA Code Explanation

Dim iSheet As Worksheet

Declaring the variable for 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 if 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.

Read More: How to Autofilter Values Not Equal to a Certain Value with VBA in Excel


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 that 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: How to Autofilter and Copy Visible Rows with Excel VBA


Conclusion

To conclude, this article showed you 4 easy and quick ways how 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

Sanjida Ahmed

Sanjida Ahmed

Hello World! This is Sanjida, an Engineer who is passionate about researching real-world problems and inventing solutions that haven’t been discovered yet. Here, I try to deliver the results with explanations of Excel-related problems, where most of my interpretations will be provided to you in the form of Visual Basic for Applications (VBA) programming language. Being a programmer and a constant solution seeker, made me interested in assisting the world with top-notch innovations and evaluations of data analysis.

We will be happy to hear your thoughts

Leave a reply

ExcelDemy
Logo