logo
search
VBA & Macro Problems

How to Automatically Sort Excel Data When New Values Are Added via VBA

Guest WriterGuest Writer Oct 7, 2026 870 views

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.

How to Automatically Sort Excel Data Using VBA Macros
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Insert a Standard Module

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.

3
Create the SortData 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.

4
Trigger on Workbook Open

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.

5
Trigger on Worksheet Activate

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.

6
Save as a Macro-Enabled Workbook

Go to 'File' > 'Save As', choose your save location, and ensure the 'Save as type' dropdown is set to 'Excel Macro-Enabled Workbook (*.xlsm)'.

Create a VBA Macro to Sort Data on Open and Activate
File Format Requirement: If you save your file using the standard .xlsx format, all VBA macro code will be deleted upon closing. You must use the .xlsm format.
Manage Data Efficiently with WPS Spreadsheet

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. 1. Open your macro-enabled file: Launch WPS Spreadsheet and open your existing .xlsm workbook.
  2. 2. Enable the Developer tab: Navigate to the 'Developer' tab in the top ribbon to access macro security and editing tools.
  3. 3. Run or edit macros: Use the 'Macros' or 'Visual Basic' button to view, edit, and execute your automated sorting scripts effortlessly.
  4. 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.
Fully compatible with Microsoft Excel (.xlsx) and macro-enabled (.xlsm) formats.Supports advanced data sorting, filtering, and automated tasks.Free, lightweight, and incredibly fast for daily spreadsheet processing.Intuitive tabbed interface that requires zero learning curve to migrate.
microsoft office alternative - wps office

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.