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)) |
Now we need to setup the sheet where we need the filtered data to be displayed. Headings were put in cells B5 to B8 and the drop down (using the Data Validation—List) feature was put in cells C5 to C8. Now we need to create or criteria fields in the RawData sheet, this is a requirement and cannot be any place else. When you use the Advanced Filter dialog box and try to place the output onto another sheet Excel will display a message saying “You can only copy data to the Active Sheet”. We will overcome this limit by using VBA and telling Excel where to put the filtered data. I used the cells M1 to P1 to define the headings and cells M2 to P2 to get the actual options from the “Filter Sheet”
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
Want to lean more? Consider joining our VBA Classes
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.
21 Responses to “Make a Pivot Table in Excel [15 Second Tutorial]”
[...] First step is to create Pivot Table. Here is a video tutorial on making pivot tables. [...]
Dear Sir,
I would like to study pivot table from basics detailed.Pls send me the tips of that
merccccccccccccccccccccccccccccccccccccc
[...] Click here to see a video tutorial of making pivot tables in excel | One more Pivot Table Video Tutorial [...]
it is very useful
hello sir
please send me some information of pivotable. and how to use it
because I am beginer so how can i learn it the pivotable.
and sir will you send me pivotable data because i have no data and i don't
know how to applyed for the pivotable. give some videos so I learn quikely.
thank you
and my best
anticipation
Hi,
This tutorail was helpful. If possible can you please email me some data, so I can create another pivot table? I am trying to learn ASAP and need some info.
Thank you
@Keri
Have a look at FakeNameGenerator.com
Top of the day to you. can you pls send me a detailed tutorial on the pivot table tutorial? Thanks.
Hi,
please tell me how to use Pivot table.
@Karthick, Yemi
Have a read of: http://chandoo.org/wp/2009/08/19/excel-pivot-tables-tutorial/
Dear Sir,
I visited this website of urs for the very first time and trust me this is fabulous.i learnt pivot table through this.Thanks a lot.In corporate world nobody gonna make u learn these things.m very grateful to u.This is a great initiative from ur side.Commendable job done.Hats off to u.For beginners also this is very helpful.The way this website makes u learn excel is very good.
Pooja
Can you please email me information to put into a pivot table and possibly a complete set of instructions? I am a beginner to pivot tables and am possibly interested in obtaining a position that requires pivot tables. thank you.
[...] Click here to see a video tutorial of making pivot tables in excel | One more Pivot Table Video Tutorial [...]
It's simply great..!!
Cool!
Fast but not furious.
Pls email me details of pivot table. I want to learn it asap because I just got a job that requires this. Thanks
it is very useful..............thanks
Sir please can u send me the complete explanation of pivot tables from basics
hello,
I wanted to know how to put pivot with emp id, emp name, emp position in one row.
Ex:-
cel A4 should have Emp id
Cell B4 should have Emp Name
Call C4 should have Emp Position
many thanks in advance
Nice information, thanx my dear.