VBA & Macro Problems

How to Auto-Generate the Next Unused ID Sub-Number in Excel

Muhammad TalhaMuhammad Talha Nov 10, 2026 870 views

Question details

The user needs to automatically generate the next available ID suffix (e.g., ID0002-1, ID0002-2) when a specific ID type is selected and an original ID is entered.

How to Auto-Generate the Next Unused ID Sub-Number in Excel
Product
Excel
Device & OS
not provided
Scenario
Data entry scenarios requiring automatic sequential ID generation based on specific conditions (e.g., 'RED' type).
Observed behavior
The system should automatically scan existing IDs, find the highest suffix for the base ID, and assign the next sequential number without manual input.
Before you start

Ensure you save your workbook as a Macro-Enabled Workbook (*.xlsm) before proceeding, and verify that the Developer tab is enabled in your ribbon to access the VBA editor.

Solution 1Recommended

Use a Worksheet_Change Event Macro

Implement a VBA macro to monitor specific columns and automatically calculate the next unused ID suffix.

This method uses Excel's built-in Worksheet_Change event. When a user inputs the original ID and selects the target type (e.g., 'RED'), the macro scans the existing entries from the bottom up, identifies the highest suffix for that specific base ID, and populates the next unused sub-number.

1
Open the VBA Editor

Right-click the sheet tab where you want the IDs generated and select 'View Code' from the context menu to open the Visual Basic for Applications editor.

2
Insert the Macro Code

Copy and paste the following code into the worksheet module: Private Sub Worksheet_Change(ByVal Target As Range) Dim r As Long, s As Long, p As Long, n As Long Dim v As String, w As String If Target.CountLarge > 1 Then Exit Sub If Intersect(Range("B2:C10000"), Target) Is Nothing Then Exit Sub r = Target.Row v = Range("B" & r).Value If v = "" Or Range("C" & r).Value <> "RED" Then Exit Sub Application.ScreenUpdating = False Application.EnableEvents = False For s = r - 1 To 2 Step -1 w = Range("A" & s).Value If w Like v & "*" Then p = InStr(w, "-") If p Then n = Mid(w, p + 1) Range("A" & r).Value = v & "-" & n + 1 Exit For End If Next s Application.EnableEvents = True Application.ScreenUpdating = True End Sub

3
Save as Macro-Enabled

Switch back to the Excel interface. Go to File > Save As, and choose 'Excel Macro-Enabled Workbook (*.xlsm)' as the file type to ensure your code is saved.

4
Enable Macros

Make sure to click 'Enable Content' if prompted by the security warning when you open the workbook to allow the macro to run.

Use a Worksheet_Change Event Macro
Customize Ranges and Conditions: You can modify the range 'B2:C10000' and the target value 'RED' in the VBA code to match your specific dataset and criteria.
Advanced Data Automation

Automate Your Workflows with WPS Spreadsheet

WPS Spreadsheet fully supports advanced data processing and VBA macros (in supported versions), allowing you to automate repetitive tasks like sequential ID generation with ease.

  1. 1. Open your Workbook: Launch WPS Spreadsheet and open your macro-enabled (.xlsm) file.
  2. 2. Enable Developer Tools: Navigate to the Developer tab. If it is hidden, go to Settings to display the Developer tools.
  3. 3. Access the VBA Editor: Click on 'Macros' or 'Visual Basic' to view, edit, and run your automated scripts safely.
Fully compatible with Microsoft Excel (.xlsm) macro filesFamiliar Developer tab and VBA Editor interfaceLightweight application with fast processing speedsFree core features for everyday office tasks
microsoft office alternative - wps office

Frequently Asked Questions

Why is my Worksheet_Change macro not triggering?

This usually happens if macros are disabled in your Trust Center settings, or if 'Application.EnableEvents = False' was left triggered due to a previous code error. Restarting Excel or manually running 'Application.EnableEvents = True' in the VBA Immediate Window resolves this.

Can I change the trigger condition from 'RED' to another text?

Yes, simply open the VBA editor, locate the line 'If v = "" Or Range("C" & r).Value <> "RED" Then Exit Sub', and replace 'RED' with your desired target text.

How do I apply this to a different column?

In the VBA code, change the Intersect range (e.g., Range("B2:C10000")) and adjust the column letters in Range("B" & r).Value and Range("C" & r).Value to match your new input and condition columns.

What happens if I don't save it as a Macro-Enabled Workbook?

If you save the file as a standard .xlsx workbook, all VBA code will be permanently deleted upon saving and closing. Always use .xlsm or .xlsb when working with macros.