VBA question: How do I reset filters before setting a new filter?

bigpat

New Member
Joined
Nov 28, 2012
Messages
24
Hi,

Columns A:G in my spreadsheet contain "Yes" or can be blank and there may be rows where there is more than one "Yes".

I have a dropdown in cell K2 where I can select the names of one of these columns and then I want to filter the data based on that column.

There's probably a better way, but this works - kind of!


Code:
If Range("K2").Value = "Adult and Specialtist Rehabilitation" Then ActiveSheet.Range("$A$1:$AA$10001").AutoFilter Field:=1, Criteria1:="Yes"
If Range("K2").Value = "Adult Community" Then ActiveSheet.Range("$A$1:$AA$10001").AutoFilter Field:=2, Criteria1:="Yes"
If Range("K2").Value = "Children and Families" Then ActiveSheet.Range("$A$1:$AA$10001").AutoFilter Field:=3, Criteria1:="Yes"
If Range("K2").Value = "Corporate and Other" Then ActiveSheet.Range("$A$1:$AA$10001").AutoFilter Field:=4, Criteria1:="Yes"
If Range("K2").Value = "Dental" Then ActiveSheet.Range("$A$1:$AA$10001").AutoFilter Field:=5, Criteria1:="Yes"
If Range("K2").Value = "Learning Disability" Then ActiveSheet.Range("$A$1:$AA$10001").AutoFilter Field:=6, Criteria1:="Yes"
If Range("K2").Value = "Tooth and Tissue Bank" Then ActiveSheet.Range("$A$1:$AA$10001").AutoFilter Field:=7, Criteria1:="Yes"

So starting from scratch, if I select "Adult and Specialtist Rehabilitation", then column A is correctly filtered. But then if select "Adult Community", it filters it to rows where column A and B both contain "Yes"

So how should I reset in between these lines?
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=8 'removes filter of col8

So basically you set the filter and specify no criteria.
I usually record what I want the macro to do and then use that code for my scripts
 
Upvote 0
Thank you. Yes I usually record macros too (the occasional time I attempt them) but I couldn't work out how to record an IF.

So based on your suggestion I inserted the following seven lines before the previous ones, which seems to have done the trick. I hadn't worked out how to reset the Criteria, but it seems you simply leave that part of the statement off.

Code:
'Clear any previous filters first
ActiveSheet.Range("$A$1:$AG$10001").AutoFilter Field:=1
ActiveSheet.Range("$A$1:$AG$10001").AutoFilter Field:=2
ActiveSheet.Range("$A$1:$AG$10001").AutoFilter Field:=3
ActiveSheet.Range("$A$1:$AG$10001").AutoFilter Field:=4
ActiveSheet.Range("$A$1:$AG$10001").AutoFilter Field:=5
ActiveSheet.Range("$A$1:$AG$10001").AutoFilter Field:=6
ActiveSheet.Range("$A$1:$AG$10001").AutoFilter Field:=7

You live and learn! Thank you.
 
Upvote 0
You could replace all those lines with
Code:
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
 
Upvote 0
Thanks. Yes I did play around with that on an earlier attempt but there are a couple of other columns that I hadn't mentioned and I want to retain filters on them.
 
Upvote 0
OK, in that case, if your interested another option would be
Code:
   Dim i As Long
   For i = 1 To 7
      ActiveSheet.Range("A1:AG1").AutoFilter i
   Next i
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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