VBA & Macro Problems

How to Create Sequential Numbers and Sort Data with Excel VBA

Kushani NimanthikaKushani Nimanthika Oct 28, 2026 868 views

Question details

The user needs to generate a sequence of serial numbers (1 to n) in one column based on the number of populated rows in a reference column, and then sort the dataset using a specific field via VBA.

How to Create Sequential Numbers and Sort Data with Excel VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
Automating data entry and organization by generating sequential IDs for a dynamic list of materials and subsequently sorting the dataset by revenue.
Observed behavior
The user wants to achieve an automated state where a VBA macro accurately counts populated rows, outputs serial numbers from 1 to the last row, and sorts the range accordingly.
Before you start

Ensure your Excel workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled the Developer tab in your ribbon to access the VBA editor.

Solution 1Recommended

Use a VBA Macro to Generate Serial Numbers and Sort Data

This VBA script finds the last row of your reference column, populates sequential numbers in the target column, and sorts the dataset in descending order.

Before running the code, confirm which column contains your reference data (e.g., Column N) and which column should display the sequential numbers (e.g., Column Q). Determine if you want the data sorted before or after the serial numbers are applied, as sorting after will scramble the sequential order of the generated numbers.

1
Open the VBA Editor

Press Alt + F11 in Excel to open the Visual Basic for Applications editor. Go to Insert > Module in the top menu to create a new module window.

2
Write the Sequencing Code

Declare your variables and use `Cells(Rows.Count, "N").End(xlUp).Row` to find the last populated row in Column N. Create a `For` loop from row 2 to the last row, setting the value of Column Q to increment sequentially (e.g., `Cells(i, "Q").Value = i - 1`).

3
Add the Sort Method

Below your numbering loop, specify your data range and use the `Range.Sort` method. Set `Key1` to your sorting column (e.g., Revenue) and `Order1` to `xlDescending`.

4
Run the Macro

Press F5 or click Run > Run Sub/UserForm from the toolbar to execute the code. Return to your worksheet to verify the sequential numbers are generated and the data is sorted properly.

Use a VBA Macro to Generate Serial Numbers and Sort Data
Execution Order Matters: If you want the serial numbers in Column Q to remain perfectly sequential (1, 2, 3...) from top to bottom, ensure your macro performs the Range.Sort operation *before* the loop generates the numbers.
Efficient Spreadsheet Automation

Automate Data Tasks with WPS Spreadsheet

WPS Office provides powerful spreadsheet capabilities, including robust support for macros and advanced formulas, allowing you to easily generate sequential numbers and sort large datasets without friction.

  1. 1. Download and Install WPS Office: Get WPS Office from the official website and install it on your device for free.
  2. 2. Open Your Workbook: Launch WPS Spreadsheet and open your existing .xlsx or .xlsm file containing your data.
  3. 3. Utilize Advanced Tools: Navigate to the Developer tab to access macro tools, or use built-in functions like SEQUENCE and Custom Sort to organize your data instantly.
  4. 4. Save with Compatibility: Save your automated spreadsheet perfectly intact with full Excel format compatibility.
Seamless compatibility with Microsoft Excel (.xlsx and .xlsm) formats.Built-in developer tools and advanced formula support to automate repetitive data tasks.Lightweight application that runs smoothly on Windows, Mac, and Linux.Free to use with an intuitive, tabbed user interface for easy multitasking.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA macro fail to find the correct last row?

This usually happens if there are blank cells scattered within your reference column. Using the method `Cells(Rows.Count, "N").End(xlUp).Row` ensures it finds the true last populated row by scanning from the very bottom of the sheet upwards, bypassing empty cells.

Can I generate sequential numbers without using VBA?

Yes. You can use the `SEQUENCE` function (e.g., `=SEQUENCE(COUNTA(N:N)-1)`) or double-click the AutoFill handle in the bottom-right corner of a cell in Excel. However, VBA is preferred when automating a multi-step process like numbering and immediately sorting the data.

How do I modify the VBA code to accommodate header rows?

If row 1 contains headers, set your loop to start at row 2 by writing `For i = 2 To LastRow`. To make sure the first data row gets the serial number 1, subtract 1 from your loop counter when assigning the value, such as `Cells(i, "Q").Value = i - 1`.

Does sorting the data mess up the generated serial numbers?

If you generate the serial numbers first and then sort the entire range including the serial number column, the numbers will move with their respective rows and look scrambled. If you want the numbers to remain sequential (1 to N) physically down the column, perform the sort operation *before* generating the serial numbers in your macro.