If statement for detecting autofilter, even when nothing has been filtered
Asked Answered
S

2

6

I've had a search around for this but havn't found anything yet. To clarify, I'm looking for a simple if statement for applying an autofilter if none exists, or do nothing if there is one already in place.

I thought the solution would be:

If ActiveSheet.FilterMode = False Then
    Selection.AutoFilter
end if

but this only detects filtered data. If a filter is already in place and you execute this line...

Selection.AutoFilter

The filter vanishes, and my code has a bad day. Is there a simple solution to this?

Slatternly answered 10/4, 2015 at 15:32 Comment(0)
S
13

You were close:

If ActiveSheet.AutoFilterMode = False Then Selection.AutoFilter
Sabir answered 10/4, 2015 at 15:37 Comment(0)
F
3

There is a difference between .AutoFilterMode and .FilterMode.

.AutoFilterMode becomes True if the AutoFilter drop-down arrows are currently displayed on the worksheet.

.FilterMode becomes True only after the drop-down arrows are used to create a filter, or after filters are created in VBA code.

And so .AutoFilterMode must become true before .FilterMode.

Faulkner answered 22/2, 2024 at 15:50 Comment(1)
that clears up a lot thank youSlatternly

© 2022 - 2025 — McMap. All rights reserved.