Clearing Excel AutoFilter using VBA

blafarm

Board Regular
Joined
Oct 14, 2018
Messages
59
I am using this VBA code to refresh my entire workbook at specific time intervals.

As you can see, it is currently set to refresh every 60 minutes.

Code:
Public RunWhen As Double
Public Const cRunIntervalMinutes = 60
Public Const cRunWhat = "Workbook_RefreshAll"
 
Sub StartTimer()
    RunWhen = Now + TimeSerial(0, cRunIntervalMinutes, 0)
    Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _
         schedule:=True
End Sub
 
Sub StopTimer()
   On Error Resume Next
   Application.OnTime earliesttime:=RunWhen, _
       procedure:=cRunWhat, schedule:=False
End Sub
 
Sub Workbook_RefreshAll()
    Application.CalculateFullRebuild
    ActiveWorkbook.RefreshAll
    Call StartTimer
End Sub

The code works great, however there’s one thing I wish it could also do:

1. Any time the sheet is opened, and/or
2. Any time this VBA code executes the sheet refresh process

I would like the AutoFilter to be "Cleared" (not disabled or turned off) as in the screen grab below:

Clear.jpg




Thanks in advance for any assistance.

Cheers
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Code:
Worksheets("Sheet Name").AutoFilter.ShowAllData
Or to prevent an error if autofilter is turned off:
Code:
With Worksheets("Sheet Name")
    If .AutoFilterMode Then .AutoFilter.ShowAllData
End With
 
Upvote 0
Hi,
You can check the state of the Autofilter and decide what to do

Code:
    With ActiveSheet
        If .AutoFilterMode = True And .FilterMode = True Then
            MsgBox "They are visible and in use"
            .ShowAllData
        ElseIf .AutoFilterMode = True Then
            MsgBox "They are visible but not in use"
            'do nothing
        Else
            MsgBox "They are not visible or in use"
            'do nothing
        End If
    End With
 
Upvote 0
Thank you both for your responses.

Unfortunately, I am a complete novice in terms of VBA, so I don't know how to properly integrate something like this into my existing VBA code:

Code:
With Worksheets("Sheet Name")
    If .AutoFilterMode Then .AutoFilter.ShowAllData
End With

But the other issue is that I would also like the sheet to open with with the AutoFilter cleared -- regardless of the state in which it was last saved and closed.

That leads me to believe that I need two statements -- one to clear AutoFilter 'on open' -- and one to clear AutoFilter every time the sheet is refreshed by the VBA code in my first post.

I would be very grateful if some kind soul would insert those two statements into the correct locations of my existing VBA -- so that I don't screw it up.

Thanks very much in advance.
 
Last edited:
Upvote 0
To clear filters on open try
Code:
Sub Workbook_open()
With Worksheets("Sheet Name")
    If .AutoFilterMode Then .AutoFilter.ShowAllData
End With
end sub
 
Upvote 0
Thank you all.

Question:

If I only wanted to REAPPLY the filter when the Workbook was first opened -- or when it was refreshed via the VBA in my first post - how might that change the code?

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,210
Members
448,874
Latest member
b1step2far

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