VBA & Macro Problems

Create a Sequential Work Order Number Macro in Excel

Algirdas JasaitisAlgirdas Jasaitis Nov 27, 2026 868 views

Question details

Create an Excel macro that assigns the next work order number when a shared template opens without giving two network users the same number.

Create a Sequential Work Order Number Macro in Excel
Product
Microsoft Excel
Device & OS
not provided
Scenario
Multiple users create work orders from a shared Excel template.
Observed behavior
Each new work order needs a unique sequential number from a central source.
Before you start

Confirm that all users can reach a central SQL Server database and that their accounts can update the counter. Choose the destination worksheet and cell, and make sure macros can run in the template.

Solution 1Recommended

Use a central SQL Server counter in an Excel Workbook_Open macro

Use an atomic database update to assign different numbers even when several users open the template at the same time.

This example uses Windows authentication and the Microsoft OLE DB Driver for SQL Server on each user's computer. A database administrator must create the counter and grant users the necessary access.

The database update is atomic, so concurrent requests cannot receive the same counter value. Numbers may have gaps if a number is reserved but the workbook is not saved.

1
Create the shared counter

In SQL Server Management Studio, connect to the shared database and run: CREATE TABLE dbo.WorkOrderSequence (SequenceName varchar(50) NOT NULL PRIMARY KEY, LastNumber int NOT NULL); INSERT INTO dbo.WorkOrderSequence (SequenceName, LastNumber) VALUES ('WorkOrder', 0); Grant users SELECT and UPDATE access to this table.

2
Open the workbook event editor

Open the template in Excel, press Alt+F11, and double-click ThisWorkbook in the VBA Project pane. Confirm that the destination worksheet is named Work Order and that cell B2 is blank in the template.

3
Add the allocation macro

Paste this code into ThisWorkbook, replacing SERVER_NAME and DATABASE_NAME with your SQL Server details: Private Sub Workbook_Open() Dim cn As Object Dim rs As Object Dim target As Range On Error GoTo Failed Set target = ThisWorkbook.Worksheets("Work Order").Range("B2") If Len(target.Value) > 0 Then Exit Sub Set cn = CreateObject("ADODB.Connection") cn.Open "Provider=MSOLEDBSQL;Server=SERVER_NAME;Database=DATABASE_NAME;Integrated Security=SSPI;" Set rs = cn.Execute("SET NOCOUNT ON; UPDATE dbo.WorkOrderSequence SET LastNumber = LastNumber + 1 OUTPUT INSERTED.LastNumber WHERE SequenceName = 'WorkOrder';") If rs.EOF Then Err.Raise vbObjectError + 1000, , "Work order counter not found." target.Value = rs.Fields(0).Value rs.Close cn.Close Exit Sub Failed: MsgBox "A work order number could not be assigned: " & Err.Description, vbExclamation On Error Resume Next If Not rs Is Nothing Then rs.Close If Not cn Is Nothing Then cn.Close End Sub

4
Save and test the template

Use File > Save As to save the file as an Excel Macro-Enabled Template (.xltm). Create new workbooks from it on two computers at nearly the same time and check that Work Order!B2 contains different numbers. Save and reopen each workbook to confirm its existing number stays unchanged.

Use a central SQL Server counter in an Excel Workbook_Open macro
Keep the template cell blank: The macro leaves a nonblank B2 unchanged so reopening a saved work order does not allocate another number. If macros are disabled or the database is unavailable, no new number is assigned; users must resolve the issue before issuing the work order.
Free Microsoft Office alternative

Edit Work Order Spreadsheets with WPS Office

WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface. Open compatible Excel files to work with existing work order layouts and move between Office applications more easily. It does not replace the Excel-specific macro and SQL Server setup above.

Edit work order templates in a familiar spreadsheet interfaceSupports Microsoft Excel formats such as XLSXFree and lightweight for everyday spreadsheet workOpen existing Office files without recreating their layouts
microsoft office alternative - wps office

Frequently Asked Questions

Why not store the next number in the Windows Registry?

Registry values are local to a computer or user, so different network users can generate the same number. Use a central counter that all users access instead.

How does the macro prevent two users from receiving the same number?

The SQL Server UPDATE increments and returns the counter in one atomic operation. Concurrent requests are handled so each successful update receives a different value.

Where do I change the cell that receives the work order number?

In the ThisWorkbook code, change Worksheets("Work Order").Range("B2") to the desired worksheet and cell. Keep that destination blank in the template.

Does the text in an InputBox determine the destination cell?

No. The text inside InputBox is the prompt shown to the user. The destination is determined by the VBA assignment, such as a value written to Range("B2").

Why might there be gaps in the work order numbers?

A number is reserved when the database counter is updated. If the user closes the new workbook without saving it, or the cell cannot be updated afterward, that number is not reused.