In Excel: paste Sub ApplyAutoFilter() into the VBA editor (Alt + F11 → Insert ▸ Module), press F5 to run, and save as .xlsm to keep the macro.
VBA macro: AutoFilter with VBA
Sub ApplyAutoFilter()
Dim ws As Worksheet
Set ws = ActiveSheet
' Remove existing filter first, then apply new one
If ws.AutoFilterMode Then ws.AutoFilterMode = False
ws.Range("A1").AutoFilter Field:=1, Criteria1:=">=100"
End SubField:=1 filters column A. Change Criteria1 to any value, comparison operator, or wildcard. Remove the filter by calling ws.AutoFilterMode = False.
How to run this macro
- Press Alt + F11 to open the VBA editor.
- Insert > Module.
- Paste the code above.
- Press
F5, or close the editor and run it from Developer > Macros. - Save the file as .xlsm so the macro is kept.
Press Alt + F11 to open the VBA editor.
Insert ▸ Module.
Paste the code above.
Press F5 (or close the editor and run it from Developer ▸ Macros).
Save the file as .xlsm so the macro is kept.
What this does
Sub ApplyAutoFilter() is a ready-to-run VBA macro that handles apply or remove an AutoFilter using VBA without manual effort. VBA (Visual Basic for Applications) is Excel's built-in automation language: it can read every cell, loop through thousands of rows, and make decisions in milliseconds. To use it, open the VBA editor with Alt + F11, create a Module, paste the code, and press F5. The AutoFilter with VBA macro will run immediately — no add-ins, no external tools, just Excel doing exactly what the code tells it to do. Keep the inputs visible and clearly labelled and the whole thing stays auditable — anyone who opens the file later, including you, can see at a glance exactly what feeds the result and change one assumption without hunting through the formula. The difference between a quick fix and a sheet you can trust is the extra minute you spend validating “excel vba autofilter”. Start on a copy or a tiny sample, keep the affected filter visible, and compare the result with the tool above before you touch the real workbook. When this is a ribbon command, the selection matters more than the button: confirm the range, apply the command, then spot-check the output before saving. The point is a data step that keeps your analysis trustworthy, but the practical win is that someone else can open the file and understand what happened without asking you.
A worked example
Scenario: you have a workbook with 500 rows of sales data and you need to apply or remove an AutoFilter using VBA. Without a macro you would scroll through every row manually, which is error-prone and slow. Instead, open the VBA editor (Alt + F11), insert a Module (Insert ▸ Module), paste Sub ApplyAutoFilter() into the code window, and press F5 to run. The macro processes all 500 rows in under a second, apply or remove an AutoFilter using VBA exactly as specified by the code. On a Mac, use Option+F11 to open the editor if Alt+F11 does not respond. Reach for the AutoFilter with VBA macro whenever apply or remove an AutoFilter using VBA is a task you repeat more than once, involves more than a handful of rows, or needs to be done consistently across multiple workbooks. VBA runs in milliseconds over data that would take minutes by hand, and it never makes the selection mistakes that manual workflows introduce when fatigue sets in. If there is any chance you will reuse this, drop it into a small template tab right now: a labelled input area on the left and the formula beside it, checked once against the tool above. Next time the same question comes up, the answer is a single paste away instead of a rebuild from memory.
In Google Sheets
If you are in Google Sheets rather than Excel, the good news is that the formula shown here is identical and the workflow barely changes — menus sit across the top instead of in a ribbon, and a few function names differ slightly, but anything you build here moves across with little or no rework. Nothing on this page is behind a login: the tool runs entirely in your browser, the formula is shown in full with one-click copy, and the steps work the same on Windows and Mac. That is the whole promise here — the exact answer, a way to prove it on your own numbers, and just enough context to make it stick. Here is the takeaway for “excel vba autofilter”: copy the answer if you are busy, but if you have a spare few minutes, rebuild the example in Excel yourself with the tool above open beside it. That single pass — type it, run it, watch the result move when you change an input — is what turns a formula you found into a technique you trust. Keep your inputs labelled and referenced, never hard-coded, and the same sheet stays correct and auditable as it grows. Done that way, you will not need to look this up again, and you will be the person others ask.
Common mistakes
- Saving as .xlsx instead of .xlsm — Excel strips all VBA code when you save in the non-macro-enabled format. Always choose "Excel Macro-Enabled Workbook (.xlsm)" from the Save As dialog.
- Running the macro on the wrong sheet — Sub ApplyAutoFilter() acts on ActiveSheet by default. If you have multiple sheets open, click the correct sheet tab first before pressing Run.
- Macros are disabled — if you see a security warning bar, click Enable Content. If there is no bar at all, set File ▸ Options ▸ Trust Center ▸ Trust Center Settings ▸ Macro Settings to "Disable VBA macros with notification", which is the safe default that still lets you approve a file per workbook. Do not select "Enable all macros": it removes the only warning you get before a hostile workbook executes.
Frequently asked questions
How do I run the AutoFilter with VBA macro?
Press Alt + F11 to open the VBA editor, insert a Module (Insert ▸ Module), paste Sub ApplyAutoFilter() into the code pane, and press F5. Alternatively close the editor and run it from Developer ▸ Macros ▸ select "ApplyAutoFilter" ▸ Run.
Why won't my macro run — macros are disabled?
Excel blocks macros by default for files downloaded from the internet. Click the yellow security bar and choose "Enable Content" — that trusts this one workbook and is all that is normally needed. Leave File ▸ Options ▸ Trust Center ▸ Trust Center Settings ▸ Macro Settings on "Disable VBA macros with notification" rather than switching to "Enable all macros", which Microsoft itself marks not recommended. If the bar is red instead of yellow, the file carries Mark of the Web: close Excel, right-click the file, open Properties and tick Unblock. Also ensure the file is saved as .xlsm, not .xlsx.
Can I undo what the AutoFilter with VBA macro did?
No — VBA actions generally cannot be undone with Ctrl+Z because they bypass Excel's undo stack. Always test Sub ApplyAutoFilter() on a backup copy of the workbook before running it on live data, or add explicit validation logic inside the macro before the destructive step.