Extract data using Advanced Filter and VBA
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
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”
Macro to run advanced filter and extract data
Sheets(“RawData”).Range(“Table1[#All]“).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:= _
Sheets(“RawData”).Range(“M1:P2″), CopyToRange:=Sheets(“Filter”).Range(“B10″), Unique:=True
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.
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.
Leave a Reply
|Excel Links – Lets meet in Chennai this Sunday edition||Macros for Automatically Implementing Modeling Best Practices|