In this post we will learn how to use the Advanced Filter option using VBA to allow us to filter our data on a separate sheet. This has been requested by a lot of our readers and here is how we will use them.
What we need to get this done.
1. Some data that we need filtering on.2. Define what options we need as drop down lists
3. A cup of coffee
In the sample data, I have defined 4 options to be available as drop down list; this has been done by creating a new sheet called as “Master”. I then copied the existing columns data into this sheet and used the Remove Duplicates feature to get the unique list of items that was required for the drop downs.
The named ranges were created using the INDEX function as shown below
Named Range | Formula |
prd | =Master!$A$2:INDEX(Master!$A:$A,COUNTA(Master!$A:$A)) |
rgn | =Master!$B$2:INDEX(Master!$B:$B,COUNTA(Master!$B:$B)) |
cust | =Master!$C$2:INDEX(Master!$C:$C,COUNTA(Master!$C:$C)) |
agnt | =Master!$D$2:INDEX(Master!$D:$D,COUNTA(Master!$D:$D)) |
Cells | Formula |
M2 | =Filter!C5 |
N2 | =Filter!C6 |
O2 | =Filter!C7 |
P2 | =Filter!C8 |
Macro to run advanced filter and extract data
Sub FilterData()
Sheets("Filter").Select
Range("B10").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Clear
Sheets(“RawData”).Range(“Table1[#All]“).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:= _
Sheets(“RawData”).Range(“M1:P2″), CopyToRange:=Sheets(“Filter”).Range(“B10″), Unique:=True
Columns.AutoFit
Range(“B10″).Select
End Sub
First we ensure the current filtered data (in any) is cleared out before we run the code again and then we get the new filtered data from cell B10 onwards. Now let’s understand the actual code that filters our data here.
Sheets("RawData").Range("Table1[#All]").AdvancedFilter
Action:=xlFilterCopy,
CriteriaRange:=Sheets("RawData").Range("M1:P2"),
CopyToRange:=Sheets("Filter").Range("B10"),
Unique:=True
We converted our raw data into an excel table (Structured Reference Structured Reference), by doing this we no longer need to know how many rows our data actually goes down to, the “Table1[#All]” takes care of that for us.
We also need to specify that our data is in another sheet and we are trying to run Advanced Filter on that data range, this is done using the first line ” Sheets(“RawData”).Range(“Table1[#All]“).AdvancedFilter “.
Next we specify the action that we need which is Copy in our case, the other option is “xlFilterInPlace” which would filter right on our data itself.
Then we have specified the Criteria Range (which needs to be on the same sheet where the data is).
And finally we have specified where the output has to be sent to by using : “CopyToRange:=Sheets(“Filter”).Range(“B10″)”
We have also made sure that only Unique records are returned to us by turning Unique:=True.
Download Advanced Filter Demo File
Click here to download the demo file & use it to understand this technique.Do you use Advanced filters to extract sub-sets of data?
Advanced filters are very powerful and very simple to setup. I use them often to quickly extract what I want.What about you? Do you use them often? Please share your experiences, techniques & ideas using comments.
Learn more about extracting / consolidating data using VBA
Data extraction and consolidation are one of the most common activities done by reporting professionals & analysts. No wonder we speak about these areas a lot here too. Please check out these pages to learn more:- Split an excel file in to many using Advanced Filters & VBA [with video]
- Move data from one sheet to many using VBA
- Split text on new line using VBA
- Consolidate data from multiple files in to one using VBA Macros
If you would like to learn more about VBA programming, Excel automation, creation of user forms, manipulating data in Access thru Excel etc., consider joining our online VBA Classes. This step-by-step program helps you become awesome in VBA.
Click here to know more & Join our classes.