How to Automatically Sort Excel Data When New Values Are Added via VBA
Question details
The user wants to automatically sort data entries (like delivery numbers) in ascending order whenever new values are added to the Excel worksheet.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Adding new records continuously and needing the data range to remain sorted without having to perform manual sort actions each time.
- Observed behavior
- Triggering a sort immediately after a cell edit moves the active row unexpectedly, making data entry confusing. The ideal behavior is to sort the data transparently when opening the file or switching tabs.
Ensure that the Developer tab is enabled in your Excel ribbon and back up your current workbook before adding and executing custom VBA macro code.
Create a VBA Macro to Sort Data on Open and Activate
Implement a custom VBA macro to sort your data automatically when the workbook opens or when the worksheet is activated, avoiding disruptive row movements while typing.
Sorting data immediately upon a cell change (Worksheet_Change) can be highly disruptive, as pressing Enter or Tab might jump your cursor to a new, unexpected location. To create a smooth data entry experience, it is better to trigger the sorting process during natural breaks, such as when the workbook first opens or when you switch over to the specific worksheet.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer pane on the left, right-click your workbook name, select 'Insert', and choose 'Module'. This creates a standard module for your macro.
In the new module window, write a macro named 'SortData' that selects your target data region and sorts it by Column A in ascending order.
In the Project Explorer, double-click 'ThisWorkbook'. Select 'Workbook' from the left dropdown and 'Open' from the right dropdown. Type 'Call SortData' inside the Workbook_Open subroutine.
Double-click the specific worksheet (e.g., Sheet1) in the Project Explorer. Select 'Worksheet' from the left dropdown and 'Activate' from the right dropdown. Type 'Call SortData' inside the Worksheet_Activate subroutine.
Go to 'File' > 'Save As', choose your save location, and ensure the 'Save as type' dropdown is set to 'Excel Macro-Enabled Workbook (*.xlsm)'.

Sort Data and Manage Macros Seamlessly in WPS Office
WPS Spreadsheet provides robust data sorting tools and extensive compatibility with Microsoft Excel files, including macro-enabled workbooks (.xlsm). You can easily automate your repetitive data sorting tasks in a fast, lightweight environment.
- 1. Open your macro-enabled file: Launch WPS Spreadsheet and open your existing .xlsm workbook.
- 2. Enable the Developer tab: Navigate to the 'Developer' tab in the top ribbon to access macro security and editing tools.
- 3. Run or edit macros: Use the 'Macros' or 'Visual Basic' button to view, edit, and execute your automated sorting scripts effortlessly.
- 4. Use built-in sort tools: Alternatively, utilize the 'Sort' button under the 'Data' tab for quick manual sorting without needing to write any code.

Frequently Asked Questions
Why shouldn't I use Worksheet_Change to sort data immediately after typing?
Sorting data immediately upon a cell change dynamically moves the active row. When you press Enter or Tab after inputting a value, your cursor may jump to an unexpected location alongside the sorted row, making continuous data entry extremely frustrating.
Why did my sorting macro disappear after I saved and closed the file?
If you save an Excel file containing macros in the default workbook format (.xlsx), all macro code is automatically stripped out for security reasons. You must save the file specifically as an Excel Macro-Enabled Workbook (.xlsm) to retain your VBA code.
How do I make the Developer tab visible in Excel?
To show the Developer tab, right-click anywhere on the top ribbon and select 'Customize the Ribbon'. In the right-hand list of main tabs, check the box next to 'Developer', then click OK.
Can I sort data automatically without using VBA macros?
While you cannot trigger an in-place automatic sort without VBA or Office Scripts, you can use the dynamic array formula =SORT() in a separate, empty range. This will automatically generate a sorted view of your source data in real-time as you add new values.




