Clear filters in VBA

vavs

Well-known Member
Joined
Jul 2, 2004
Messages
514
I have a spreadsheet that goes through several macros in order to update. One of the macros did not complete properly due to a filter that was applied to the sheet. Is there a way to add this to my first macro in the string, whereby I query each sheet and clear the filters before I start?

I have tried this and I get a compile error:

Rows(1). Select
Selection.Autofilter
Selection.Autofilter

I don't know how to turn it on and then off.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
vavs,

This may help you get started in the right direction.

Code:
Dim wks As Worksheet
   For Each wks In Me.Worksheets
        wks.AutoFilterMode = False
    Next wks


Or, toggle AutoFilter on/off:

Code:
Sub MyAutoFilter()
With ActiveSheet
  If .FilterMode Then
    .AutoFilter
  Else
    .UsedRange.AutoFilter
  End If
End With
End Sub
 
Upvote 0
.
.

This solves the original problem:

Code:
Sub ToggleAutoFilter()

    With ActiveSheet
        If .AutoFilterMode Then
            .AutoFilterMode = False
        Else
            .Range("I1").CurrentRegion.AutoFilter _
                Field:=22, _
                Criteria1:="<>"
        End If
    End With
    
End Sub
 
Upvote 0
I already have the following code in MY Workbook - where should I place this line? Where I have it now is throwing an error.

(you can see I tried another solution, but it didn't work, so I have it remarked out)

Private Sub Workbook_Open()


'Dim sh As Variant
'For Each sh In Worksheets
'If sh.FilterMode Then sh.ShowAllData
'Next


On Error Resume Next
Application.OnTime RunWhen, "SaveAndClose", , False
On Error GoTo 0
RunWhen = Now + TimeSerial(0, NUM_MINUTES, 0)
Application.OnTime RunWhen, "SaveAndClose", , True
End Sub




Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.OnTime RunWhen, "SaveAndClose", , False
On Error GoTo 0


ActiveSheet.ShowAllData


'Dim WS As Worksheet
' For Each WS In Worksheets
' WS.AutoFilterMode = False
' Next WS
ThisWorkbook.Save


End Sub




Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
On Error Resume Next
Application.OnTime RunWhen, "SaveAndClose", , False
On Error GoTo 0
RunWhen = Now + TimeSerial(0, NUM_MINUTES, 0)
Application.OnTime RunWhen, "SaveAndClose", , True
End Sub




Private Sub Workbook_SheetSelectionChange(ByVal sh As Object, _
ByVal Target As Range)




On Error Resume Next
Application.OnTime RunWhen, "SaveAndClose", , False
On Error GoTo 0
RunWhen = Now + TimeSerial(0, NUM_MINUTES, 0)
Application.OnTime RunWhen, "SaveAndClose", , True




End Sub
 
Upvote 0
Try;

Code:
ActiveSheet.ShowAllData

this works great for me as long as the sheet is filtered but if the sheet happens to not be filtered at the time I run the macro it throws a "run-time error '1004': ShowAllData method of Worksheet class failed" what would be a good way to add a check to see if it is filtered, run this if it is but skip it if it is not filtered?
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,453
Members
448,967
Latest member
grijken

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