Sort an Expanding Filtered Range with Excel VBA
Question details
Sort a named Excel data range that grows or shrinks without hard-coding its row numbers.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A user wants a VBA macro to sort a filtered data block after rows are added or deleted.
- Observed behavior
- The sort should use the current size of the data block rather than a fixed range.
Check that R_7 is a defined name referring to a cell in the data block and that the block has a header row. Remove completely blank rows or columns within the data, because they can interrupt CurrentRegion.
Sort the named range's current region with VBA
Use CurrentRegion to identify the contiguous data block each time the macro runs.
CurrentRegion expands or shrinks with the contiguous block around R_7. The sort key .Range("A1") is the first column of that region, not necessarily column A of the worksheet.
In Excel, open Formulas > Name Manager and confirm that R_7 refers to a cell within the data block you want to sort.
Open Developer > Visual Basic, choose Insert > Module, and paste: Sub Test() With Range("R_7").CurrentRegion .Sort .Range("A1"), xlAscending, Header:=xlYes End With End Sub
Return to Excel, open Developer > Macros, select Test, and click Run. Run it again after the data block changes when you need to refresh the sort.
Choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm) to retain the VBA code.

Sort a changing filtered data block in WPS Spreadsheets
Use WPS Spreadsheets to filter and sort a contiguous data block without entering fixed row numbers. Reapply the sort after adding or deleting rows; this is a manual alternative to the Excel VBA macro.
- 1. Open the workbook: Open WPS Spreadsheets and choose File > Open to select your Excel workbook.
- 2. Select the data block: Select the data, including its header row and all rows you want to sort. Check that newly added rows are included.
- 3. Apply a filter: Choose Data > Filter, then use the header drop-downs to set any filters you need.
- 4. Sort the data: Open the drop-down for the column you want to sort and choose ascending or descending order. After adding or deleting rows, check the selected filter range and reapply the sort.

Frequently Asked Questions
Why does CurrentRegion leave out some rows?
A completely blank row or column can mark the edge of the region. Check the data around R_7 and remove unintended blank separators before running the macro.
Which column does the macro sort?
The key .Range("A1") refers to the first column of the CurrentRegion. Change it to a cell in another region column if you want a different sort key.
Does the macro sort new rows automatically?
No. CurrentRegion is recalculated when the macro runs, but the sample does not run automatically. Use Developer > Macros to run Test again after the data changes.
How do I keep the VBA code when saving?
Choose File > Save As and save the workbook as an Excel Macro-Enabled Workbook (*.xlsm).




