Excel VBA Selection AutoFilter: Step-by-Step Guide
In this article
You can auto-filter only the rows you have selected in Excel VBA by first making sure the data is a proper range or table, then applying AutoFilter to that specific range rather than to the whole worksheet. The shortest working pattern is: select the range (including headers), then run Selection.AutoFilter Field:=1, Criteria1:="value". The catch is that Excel's built-in AutoFilter always filters the entire current region of the sheet, so if you truly need to filter a non-contiguous or isolated selection, you must copy it to a temporary range, filter that, and copy the visible rows back — or restructure the data so the selection is its own contiguous block.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac). Labels vary slightly by version and region.
- A worksheet with a contiguous block of data that has a single header row.
- Basic familiarity with the VBA editor (Alt+F11 on Windows, Option+F11 on Mac) and how to insert a module (Insert > Module).
- Macro-enabled workbook saved as .xlsm if you want to keep the code.
Step-by-Step
- Confirm the data is one contiguous block. AutoFilter works on a rectangular range with headers in the first row and no fully blank rows or columns inside it. If your data has gaps, either fill them or treat each block as a separate range.
- Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac), then use Insert > Module to add a standard module.
- Turn off screen updating and alerts at the top of your routine so the filter runs cleanly:
Application.ScreenUpdating = FalseandApplication.DisplayAlerts = False. Remember to set them back to True at the end. - Define the range explicitly. Use something like
Dim rng As Range: Set rng = Range("A1:D200")or, more robustly,Set rng = Range("A1").CurrentRegion. CurrentRegion expands to the surrounding block, which is what AutoFilter expects. - Clear any existing filter first so you do not stack criteria:
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False. Note that this removes the filter dropdowns entirely; to keep them and only clear criteria, userng.AutoFilterwith no arguments after the filter exists. - Apply the filter to the range, not to Selection. The reliable form is
rng.AutoFilter Field:=1, Criteria1:="North", where Field is the column number within the range (1 = first column). UsingSelection.AutoFilterworks only when the selection exactly matches the data block; otherwise Excel filters the whole current region anyway. - If you must filter a selection that is not the whole block, copy it to a scratch area first:
Selection.Copy Destination:=Sheets("Temp").Range("A1"), apply AutoFilter there, then copy only visible cells back usingSpecialCells(xlCellTypeVisible). This is the only honest way to filter a subset without touching the rest of the sheet. - Add multiple criteria with
Criteria1andCriteria2plusOperator:=xlAndorxlOr. For wildcards use"*text*". For "not equal" useCriteria1:="<>value". - Run the macro with F5, or assign it to a button via Developer > Insert > Button (Form Control).
- Restore settings at the end:
Application.ScreenUpdating = TrueandApplication.DisplayAlerts = True.
Common Mistakes
- Filtering Selection when the selection is a single cell. Excel silently expands to the current region, so you get a filter on more data than you intended.
- Forgetting that AutoFilter needs headers. If the first row of the range is data, Excel treats it as a header and your first record disappears from the filter list.
- Stacking filters without clearing. Running the macro twice adds criteria rather than replacing them in some patterns; clear AutoFilterMode first.
- Using Field numbers based on worksheet columns instead of range columns. Field 1 is the first column of the filtered range, not column A.
- Leaving ScreenUpdating off after an error, which makes Excel look frozen.
- Assuming AutoFilter works on non-contiguous selections. It does not; the selection is ignored and the surrounding block is filtered.
Tips
- Use
CurrentRegioninstead of hard-coded addresses so the macro survives rows being added. - Store criteria in worksheet cells and reference them (
Criteria1:=Range("F1").Value) so non-coders can change the filter without editing VBA. - Use
rng.SpecialCells(xlCellTypeVisible).Copyto extract filtered rows to another sheet. - Convert the range to an Excel Table (Insert > Table) and filter with
ListObjects("Table1").Range.AutoFilter; tables expand automatically and are less fragile than fixed ranges. - Wrap the routine in
On Error GoTo Cleanupso ScreenUpdating and DisplayAlerts are always restored.
Troubleshooting
- "AutoFilter method of Range class failed." The range is not a valid contiguous block, or it overlaps a merged cell. Unmerge cells and retry.
- Filter applies to the wrong rows. Your range is smaller than the actual data block, so Excel expanded it. Use CurrentRegion or widen the range.
- Dropdowns disappear after running. You set
AutoFilterMode = Falseand never re-applied the filter. Re-run the AutoFilter line. - Macro runs but nothing changes. The criteria string does not match any value (watch for trailing spaces and case). Test with a known value first.
- Excel freezes. ScreenUpdating was left off after an error. Press Esc, then run
Application.ScreenUpdating = Truein the Immediate window (Ctrl+G). - Mac behaves differently. Some dialog and ribbon labels differ; the VBA object model for AutoFilter is the same, but file paths and shortcut keys are not.
Frequently Asked Questions
Can AutoFilter filter only the selected cells?
No. AutoFilter always filters the entire contiguous block around the selection. To filter a subset, copy it to a temporary range, filter there, and copy the visible rows back.
Why does Selection.AutoFilter filter more rows than I selected?
Because Excel expands the selection to the current region. If the selection is one cell inside a data block, the whole block is filtered.
How do I clear a filter in VBA?
Use If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False to remove filters entirely, or rng.AutoFilter with no arguments to clear criteria while keeping the dropdowns.
Can I filter by more than two criteria?
Not directly with Criteria1 and Criteria2. For three or more values, either use an array with Operator:=xlFilterValues or add a helper column and filter on that.
Does AutoFilter work on a Table?
Yes. Reference the table's range, for example ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:="North".
Why do my headers become data after filtering?
The range's first row was not recognised as a header, usually because the header cells are blank or the range starts one row too low. Include the header row in the range.