Search code examples
vbaexcel

How can I use an if statement to turn autofilter off if it is already on otherwise leave off


I want a bit of code that turns autofilter off if it is currently turned on. I can't seem to get it working and haven't found a solution.

Thanks,

Rob


Solution

  • This is something that I use. It works with a hard-coded range, but you can pass it as a parameter as well. Pretty much, if there is autofilter , it will disable it on A2:K2.

    Public Sub CleanTheFilters()
    
        If ActiveSheet.AutoFilterMode Then
            ActiveSheet.Range("A2:K2").AutoFilter
        End If
    
    End Sub