Use VBA to Sort Multiple Excel Ranges Separated by Blank Rows
Question details
The user needs to sort multiple distinct groups of data within a single worksheet independently, where each group is separated by blank rows.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Organizing a large worksheet containing disconnected blocks of data that all require individual sorting based on a specific column (Column AA).
- Observed behavior
- The data is currently static and segmented by empty rows. Sorting the entire sheet normally mixes or ruins the data blocks, requiring an automated VBA approach to target and sort each group individually.
Before running any VBA macro, ensure you save a backup copy of your workbook, as VBA actions cannot be easily undone. Verify that every distinct data group contains the same header configuration so the sort function applies uniformly.
Use CurrentRegion Property in VBA to Loop and Sort
Write a VBA macro that uses the CurrentRegion property to automatically detect continuous data blocks, sort them by Column AA, and jump over blank rows to the next block.
The most efficient way to handle multiple disconnected ranges is to anchor a variable to the first cell of your data, then use the CurrentRegion property. This property acts identically to pressing Ctrl+A inside a data block, selecting the entire continuous range until it hits an empty row or column.
In your workbook, press 'ALT + F11' on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the VBA editor, click 'Insert' from the top menu and select 'Module' to open a new blank coding window.
Define a Range variable and set it to your first data block (e.g., Range("A1")). Create a 'Do While' loop that checks if the cell is not empty. Inside the loop, reference 'CurrentRegion', apply the 'Sort' method using Column AA as the Key1, and ensure Header is set to xlYes.
At the end of the loop, redefine your starting Range variable by offsetting it by the number of rows in the CurrentRegion plus 1 (or more, depending on your blank rows) to land on the next data block.
Close the VBA editor or press 'F5' to execute the macro. The script will step through each separated range, sorting them individually.

Use WPS Spreadsheet to Run Macros and Automate Tasks
WPS Spreadsheet fully supports VBA macros, allowing you to easily write, edit, and execute scripts to sort multiple data blocks separated by blank rows. Its intuitive Developer interface makes managing complex automation seamless.
- 1. Enable the Developer Tab: Open WPS Spreadsheet, go to the top ribbon, and verify that the 'Developer' tab is visible. If not, go to Settings to enable it.
- 2. Open the Macro Editor: Click on the 'Developer' tab, then click 'Visual Basic' or 'Macros' to open the VBA scripting environment.
- 3. Insert Your Code: Right-click your workbook name in the project explorer, select Insert > Module, and paste your CurrentRegion sorting script.
- 4. Execute and Save: Click the 'Run' button (play icon) to sort your data blocks, then save the document as a macro-enabled workbook (.xlsm).

Frequently Asked Questions
Can I sort multiple groups separated by blank rows without using VBA?
Yes, but it must be done manually. You would have to highlight each continuous data block one at a time and use the standard Sort function from the Data tab. For workbooks with dozens of groups, VBA is highly recommended to save time.
Why is my VBA macro skipping the last data block?
This usually happens if your loop's exit condition is tied to the total used rows, but the final offset miscalculates due to trailing blank rows. Ensure your loop condition checks up to the absolute last populated row in the worksheet using 'Cells(Rows.Count, 1).End(xlUp).Row'.
Does WPS Office support Excel VBA macros?
Yes, WPS Office Pro and certain business editions natively support VBA macros. You can open .xlsm files, access the Visual Basic editor, and run your automated sorting scripts seamlessly.
How do I ensure my headers aren't sorted into the data?
In your VBA Sort method parameters, explicitly set 'Header:=xlYes'. This tells Excel or WPS Spreadsheet to freeze the top row of the CurrentRegion being processed and only sort the rows beneath it.




