Bottom line: Learn how to create macro buttons that apply filters to multiple columns in a filtered range or Table.
Skill level: Intermediate
Download the Excel File
Download the example Excel file to follow along.
Macros for Filters
If you regularly apply the same filters to multiple columns in Excel, then you know how time consuming it can be. In the video above it took me 24 mouse clicks and key strokes to apply filters to three columns.
Fortunately I didn't have to scroll horizontally to find the columns. That can add additional time to the task when we are filtering a large data set with a lot of columns. My Filter Mate Add-in helps with this task of navigating filtered ranges.
Let's see how we can turn those 24 steps into one single click or keyboard shortcut by creating a macro to filter our data.
Step 1: Record or Write the Macro
The first step is to create the macro. In the video above I use the macro recorder to create the code.
The macro recorder creates the VBA code for all the actions we take in Excel. It is a great tool for creating macros for filters. The code can get complex, especially as we apply filters with multiple criteria, and the macro recorder can create most of the code for us.
Here's how to record the macro.
- Clear the filters on your sheet or Table.
- Turn the macro recorder on (Developer Tab > Record Macro button)
- Give the macro a name.
- Choose to Store macro in: This Workbook
- Click OK
- Apply filters to one or more columns using the Filter Drop-down menus
- Stop the recorder (Developer Tab > Stop Recording button)
We now have a macro that will apply all the filters. You can look at the code in the VB Editor if you'd like. I explain more about that in step 3 below.
Step 2: Create a Macro Button
Next we will create the button on the sheet and assign the macro to it. I like to use Shapes for macro buttons, but you can also use the Command Button control from the Insert drop-down on the Developer tab.
Here are the steps to create the macro button.
- Draw a shape on the sheet (Insert tab > Shapes drop-down > Rectangle shape).
- Add text to the shape (Right-click > Edit Text | or double-click in the shape).
- Assign the macro (Right-click the border of the shape > Assign Macro…)
- Select the macro from the list.
- Press OK.
When you hover your mouse over the shape it will now turn into the hand pointer cursor. Clicking the shape will run the macro.
Woohoo! We have just turned dozens of steps into a single click! 🙂
If you need to select and move/edit the shape, hold the Ctrl key while clicking the shape. This prevent the macro from running and allow you to modify the shape.
Step 3: Modify the Macro
Eventually you will want to modify this code to change the filters. Of course you can re-record the macro to get the new code, but it's also good to understand how the code works.
We can look at the code in the VB Editor by clicking the Visual Basic button on the Developer tab (keyboard shortcut: Alt+F11). Then double click the module that was added to the workbook by the macro recorder. See the video above for details.
Each line of code uses the AutoFilter method to apply the filters. The AutoFilter method has 5 parameters. These parameters allow us to specify the column that the filter will be applied to, and the criteria.
I have a series of posts on this topic, starting with The Ultimate Guide to Excel Filters with VBA Macros: AutoFilter Method. There are additional articles for different filters and data types including: blanks, text, numbers, dates, colors & icons, and clearing filters.
Important Things to Know about AutoFilter
I recommend checking out the Guide to VBA Filters mentioned above, but there are a few important things to know about the AutoFilter method.
- The AutoFilter method can be used on both regular ranges or Excel tables (VBA ListObjects). I explain more about that in the Guide to VBA Filters post.
- Each line of code clears and applies a filter to a single column. The code first clears any existing filters to the column, then applies the criteria specified in the line of code. Therefore, we cannot use multiple lines of code to apply multiple filters to the same column. All criteria for the column must be in one line of code.
- The ShowAllData method is used to clear all filters. This line can raise an error if the filters are already cleared from the sheet. Checkout my post on clearing filters with VBA to learn how to handle the error.
- The values for Criteria1 and Criteria2 are wrapped in quotation marks. Any comparison operators for greater than/less than filters (= < >) or wildcard characters (* ?) are also included inside the quotes.
Give Your Users More Buttons
Once the users of your spreadsheet use the button, they will want more of them! Fortunately it's easy to replicate the button.
Step 1: Make a copy of the macro button
The easiest way is to hold the Ctrl key, then select the shape, then drag it right or left. This will duplicate the shape.
If you hold the Ctrl and Shift keys during the process it will keep the new shape aligned with the original. Here's a quick screen cast animation that shows how to duplicate the shape.
Step 2: Create a new filter macro
You can record a new macro with different filter criteria. Or, you can copy the existing macro in the VB Editor and paste it below. Just remember to change the name of the macro and also modify the AutoFilter lines of code.
Step 3: Assign the macro
Assign the new macro to the new button. You can continue to replicate this process for commonly used filters.
The screenshot above shows an example of 5 macro buttons that apply different filters. I made it look like a slicer, but it's actually shapes assigned to macros. The file that contains this example is the file on the The Ultimate Guide to Excel Filters with VBA post.
Your users (co-workers) will love the time-saving convenience of these buttons, so expect some high fives coming your way. 🙂
Adding Macro Buttons to the Ribbon
We can also add macro buttons to the Ribbon toolbar in Excel. Checkout my 4-part video series on the Personal Macro Workbook to learn more. In that series I explain how to add custom buttons to the ribbon for use on any open workbook.
Those ribbon buttons won't necessarily travel with the workbook, unless you use Ribbon XML to modify the workbook. I'll do a follow-up post on the differences between those techniques.
Free Webinar on Macros & VBA
If you are interested in learning more about macros, I'm currently running my free webinar called “The 7 Steps to Getting Started with Macros & VBA”. It's running all next week, and it's absolutely free to register.
During the webinar I explain why you might want to learn VBA, and a lot of the basic coding concepts that will help you get started. I jump into Excel and the VB Editor and walk through how to write and run our first macro. Even if you have been using VBA for awhile, I'm sure you will learn some new tips.
Click here to learn more and register for the webinar
Please leave a comment below with any questions or suggestions. Thanks! 🙂
Jon, this is fabulous!
Thanks Sandeep! I appreciate your support! 🙂
Very good explanation. Thank you
Thanks Jfdelvi! 🙂
Lovely style making it so simple to understand!!!!
Great job……………..keep it up jon!
Wonderful! So simple made it to understand!!!
Great job Jon! Keep it up man!
Thank you so much Jon, very interesting, looking forward to your VBA cause tomorrow.
Cheers from Down Under
Thanks so much Delville! 🙂
Thank you Jon. I found what i was searching for. And it is very easy to understand and use.
Hi,
I wanted to use a Checkbox instead of a Button so that it works the same way when its Checked and returns back to the Start when Unchecked.
Thanks
Hello Jon,
I went through the webinar, It is really very interesting and knowledgeable.
I am pleased to learn from you as I find you very clear and helpful towards teaching with clear goals.
I need some help in writing a specific macro for my process. It is easy but i dont know how to do that. Can you please guide me in doing that so that i learn from you and teach others on behalf of you.
Please revert back to me through email – [email protected] if you are okay to extend the helping hand.
Thank you very much.
Great job once again Jon.
I do not find the excel you prepared attached anywhere here for me to download.
please help
Its down below his video How to create macro.
Hi there Jon
Thanks for the tutorial.
I am looking to filter multiple columns in a simple sheet which is simple enough with the tick boxes for some columns but want to know how to filter by several job keywords within a single column – i.e. ‘sustainability’, ‘environment’, ‘csr’, ‘materials’, etc. Is this possible? Basically any job title with any of the above keywords, I wanted filtered.
Equally, on one of the other columns, I want to filter by company name but just programme in a load of leading electronics manufacturers, so anyone with ‘cisco’, ‘apple’, ‘ibm’, etc in their company name.
If this is possible is there any limit to the number of keywords I can programme the macro to filter?
Thanks very much in advance.
Nick
Hi John,
Is there a way to add a filtering macro to a pivot table in excel?
Jon,
I would like to filter data for a school district by school. I would like to create this auto filter and copy to a new spreadsheet in macro.
We have 32 schools and this is a tedius task sharing test data with our schools.
Hi,
I have a sheet with multiple filter buttons and want to allow a user to select multiple filter buttons. Similar to the was you can “add current section to filter” with a normal autofilter. Is that possible?
Hi,
if there is a sheet that is nog visible (hide): the macro gives an error.
Is there a possibility that the macro only works for the active sheets?!
I AM UNABALE TO CREAT THAT BOTTON
i have done all work almsot but i am unable to creat botton from where i can search related items,kindle tel me short and easy way how can i create botton
thanks
I want to use a command button that disables after one click and enables 7 days later at a specific time. Is this possible
Hi. I created a macro to filter a column. If I add additional rows they do not appear in my filter. Is there a work around for this.
Thanks
Hello Jon,
thanks for sharing this.
Do you happen to know how to call the macro when the “auto filter triangle” is ticked?
I would like to replace Excel’s default filter popup with a custom popup which allows me to key in multiple filter entries, instead of just 1 entry.
Thanks