Check if Autofilter is on when opening workbook, if it is off, then turn it on

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
572
Office Version
  1. 365
Platform
  1. Windows
Hi,

I think this should be easy, but the more I tinker with it, I get the same results. The code below is called from the workbook open event. I want it to see if "Autofilter" is turned on for my "table1". If it is on, I want it to stay on, if it is off, I want it turned on. The code I have below just turns it on if it is off or turns it on if it is on when I open the workbook. Please help.


Thanks, SS

VBA Code:
Sub AutoFilterCheck()

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

    Else
    
        MsgBox "Auto Filter is turned off"
   
        Worksheets("PO History").ListObjects("Table1").Range.AutoFilter
        
    End If
    
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
This instruction turns it on if it is off and if it is on it turns it off 🫤

VBA Code:
Worksheets("PO History").ListObjects("Table1").Range.AutoFilter

Try this:
VBA Code:
Sub AutoFilterCheck()
  Dim tbl As ListObject
  Dim st As Boolean
  
  Set tbl = Worksheets("PO History").ListObjects("Table1")
  On Error GoTo turn_on_filter
  st = tbl.AutoFilter.FilterMode
    MsgBox "Auto Filter is turned on"
    
  Exit Sub
  
turn_on_filter:
  MsgBox "Auto Filter is turned off"
  tbl.Range.AutoFilter

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top