VBA & Macro Problems

Sort an Expanding Filtered Range with Excel VBA

Tauseeq MagsiTauseeq Magsi Nov 27, 2026 869 views

Question details

Sort a named Excel data range that grows or shrinks without hard-coding its row numbers.

Sort an Expanding Filtered Range with Excel VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Check the named range

In Excel, open Formulas > Name Manager and confirm that R_7 refers to a cell within the data block you want to sort.

2
Add the macro

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

3
Run the sort

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.

4
Save the macro

Choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm) to retain the VBA code.

Sort the named range's current region with VBA
Check the region and sort key: Header:=xlYes treats the first row of the region as headers. Change the sort key if you need a different column, and ensure adjacent data is separated from the block so CurrentRegion does not include it.
Sort data in WPS Spreadsheets

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. 1. Open the workbook: Open WPS Spreadsheets and choose File > Open to select your Excel workbook.
  2. 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. 3. Apply a filter: Choose Data > Filter, then use the header drop-downs to set any filters you need.
  4. 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.
Filter and sort a changing data block without writing VBAOpen and edit Microsoft Excel XLSX and XLS filesWork in a familiar spreadsheet interfaceUse WPS Office for free
microsoft office alternative - wps office

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).