Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA add Autofilter if it doesn't exist

How do I check if a range already has autofilters, and apply them if not.

At the moment I am simply using

Range("A1:N1").AutoFilter

However if that range already has Filters on it turns them off.

I have searched for this and found lots of solutions to clearing and reseting autofilters, but none about actually checking if filters are actually applied.

like image 654
Tim Wilkinson Avatar asked Jan 07 '23 08:01

Tim Wilkinson


2 Answers

Your current solution should work fine but you could use an If statement like

If Sheets(curSheet).AutoFilterMode = True Then

'Do Nothing

Else

Sheets(curSheet).Range("A1").AutoFilter

End If
like image 110
BerticusMaximus Avatar answered Jan 15 '23 18:01

BerticusMaximus


Rather than checking I just turned off AutoFilter before reapply.

Sheets(curSheet).AutoFilterMode = False
Range("A1:N1").AutoFilter
like image 35
Tim Wilkinson Avatar answered Jan 15 '23 19:01

Tim Wilkinson