VBA & Macro Problems

Create an Excel VBA Incident Number with Year, Month, and Sequence

Bushra ParveenBushra Parveen Nov 27, 2026 869 views

Question details

Generate incident numbers in an Excel 2013 VBA user form using the current year and month and a continuing five-digit sequence.

Create an Excel VBA Incident Number with Year, Month, and Sequence
Product
Microsoft Excel 2013
Device & OS
not provided
Scenario
A user form records incidents and needs to assign each new entry an identifier such as Inc-202601-00001.
Observed behavior
Each new identifier should increment the previous sequence while retaining leading zeros and displaying the current year and month.
Before you start

Identify the worksheet and column that store incident numbers, and confirm that the latest number is the last populated entry in that column. The example uses column A, a header in A1, and a user-form text box named txtIncidentNumber.

Solution 1Recommended

Generate and record the next incident number in a VBA user form

Use this approach when incident numbers are stored in column A and the sequence should continue across months.

The code reads the last incident number, increments its final five digits, and combines the result with the current year and month. It starts at 00001 when there are no entries below the header.

This example assumes the last populated row contains the most recently assigned number. It does not coordinate simultaneous edits by multiple users.

1
Prepare the worksheet

In Excel, name the incident worksheet Incidents, enter a header in A1, and store existing incident numbers in column A. Change the worksheet name in the code if yours is different.

2
Open the user-form code

Press Alt+F11, select your user form in the VBA editor, and open the Click event for the button that saves an incident. Add a text box named txtIncidentNumber or change that name in the code.

3
Add the numbering code

In the button's Click event, use the following example. Integrate it with your existing validation and incident-saving code so the number is assigned only when an incident is ready to be recorded: Private Sub cmdSave_Click() Dim ws As Worksheet Dim lastRow As Long Dim nextRow As Long Dim lastID As String Dim nextSeq As Long Dim newID As String Set ws = ThisWorkbook.Worksheets("Incidents") lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row If lastRow < 2 Then nextSeq = 1 nextRow = 2 Else lastID = CStr(ws.Cells(lastRow, 1).Value) If Len(lastID) <> 16 Or Left$(lastID, 4) <> "Inc-" _ Or Mid$(lastID, 11, 1) <> "-" _ Or Not IsNumeric(Right$(lastID, 5)) Then MsgBox "Check the last incident number in column A." Exit Sub End If nextSeq = CLng(Right$(lastID, 5)) + 1 nextRow = lastRow + 1 End If If nextSeq > 99999 Then MsgBox "The five-digit incident sequence is exhausted." Exit Sub End If newID = "Inc-" & Format(Now, "yyyymm") & "-" & Format(nextSeq, "00000") ws.Cells(nextRow, 1).Value = newID Me.txtIncidentNumber.Value = newID ' Write the remaining incident fields to nextRow here. End Sub

4
Save and test the form

Use File > Save As to save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Run the form, save a test incident, and check that column A and the text box show the next number; save the workbook after recording the incident.

Generate and record the next incident number in a VBA user form
Sequence and date behavior: The sequence continues when the month changes; only the YYYYMM portion changes. The code uses the computer's current date and stops rather than issuing a six-digit sequence after 99999.
Free Microsoft Office alternative

Manage incident records in WPS Spreadsheet

WPS Office is a free, lightweight Office alternative with a familiar interface and support for Excel formats, making it easy to move spreadsheet-based incident records. It does not repair Excel's VBA user form; check macro compatibility before migrating an automated workbook.

Organize incident records and identifiers in a spreadsheetOpen and work with Microsoft Excel formats such as XLSXUse a familiar spreadsheet interface for easier migrationGet a lightweight Office suite with a free option
microsoft office alternative - wps office

Frequently Asked Questions

Does the sequence restart at the beginning of each month?

No. The example reads the last sequence in column A and increments it regardless of the month. The YYYYMM portion reflects the date when the new incident is recorded.

How are leading zeros preserved in the incident number?

The VBA expression Format(nextSeq, "00000") converts the sequence to at least five digits, so 12 appears as 00012. The example stops if the sequence exceeds 99999.

What happens if the incident list is empty?

If column A contains only the header in A1, the code writes the first incident number to A2 with the sequence 00001.

Why does the code ask me to check the last incident number?

The last populated cell in column A must have the expected 16-character structure, including the Inc- prefix, a hyphen before the sequence, and a numeric five-character ending. Correct that cell before running the button again.

How do I keep the user-form macro in my workbook?

In Excel, select File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). Enable macros only if you trust the workbook and its source.