VBA & Macro Problems

Generate Sequential Serial Numbers in Excel for Mac 2011

Natalie TaylorNatalie Taylor Nov 27, 2026 868 views

Question details

Generate serial numbers in column A based on name and information pairs in columns B and C.

Generate Sequential Serial Numbers in Excel for Mac 2011
Product
Microsoft Excel for Mac 2011
Device & OS
Mac
Scenario
A worksheet needs to reuse an assigned identifier when a name and information pair appears again and assign the next identifier to a new pair.
Observed behavior
Matching pairs should share an identifier, while new pairs should receive the next available sequential identifier.
Before you start

Place headers in row 1, names in column B, and related information in column C. The example uses identifiers such as SN-0001; check existing identifiers and adapt the prefix or numbering rules if your format differs.

Solution 1Recommended

Use VBA to assign and reuse sequential identifiers

Run a macro that preserves existing identifiers, reuses them for matching pairs, and fills blank identifier cells for new pairs.

The example treats names and information as a case-insensitive pair and ignores leading and trailing spaces. It skips rows where either field is blank.

New identifiers use the SN- prefix and a number greater than the highest existing SN- number. Existing identifiers with other formats can be reused for a matching pair but are not included when calculating the next number.

1
Open the VBA editor

In Excel for Mac 2011, open the workbook and select Tools > Macro > Visual Basic Editor. In the editor, select Insert > Module.

2
Paste the macro

Paste this code into the module: Sub AssignSerialNumbers() Const PREFIX As String = "SN-" Dim ws As Worksheet Dim lastRow As Long, r As Long, j As Long Dim maxNum As Long, suffix As String, existing As String Dim nameValue As String, infoValue As String Set ws = ActiveSheet lastRow = Application.Max(ws.Cells(ws.Rows.Count, "A").End(xlUp).Row, _ ws.Cells(ws.Rows.Count, "B").End(xlUp).Row, _ ws.Cells(ws.Rows.Count, "C").End(xlUp).Row) ' Find the highest existing number with the chosen prefix. For r = 2 To lastRow existing = Trim(CStr(ws.Cells(r, "A").Value)) If UCase(Left(existing, Len(PREFIX))) = UCase(PREFIX) Then suffix = Mid(existing, Len(PREFIX) + 1) If Len(suffix) > 0 And Not suffix Like "*[!0-9]*" Then If CLng(suffix) > maxNum Then maxNum = CLng(suffix) End If End If Next r ' Fill only blank identifiers. For r = 2 To lastRow nameValue = Trim(CStr(ws.Cells(r, "B").Value)) infoValue = Trim(CStr(ws.Cells(r, "C").Value)) If nameValue <> "" And infoValue <> "" And _ Trim(CStr(ws.Cells(r, "A").Value)) = "" Then existing = "" For j = 2 To lastRow If Trim(CStr(ws.Cells(j, "A").Value)) <> "" Then If StrComp(nameValue, Trim(CStr(ws.Cells(j, "B").Value)), vbTextCompare) = 0 And _ StrComp(infoValue, Trim(CStr(ws.Cells(j, "C").Value)), vbTextCompare) = 0 Then existing = CStr(ws.Cells(j, "A").Value) Exit For End If End If Next j If existing = "" Then maxNum = maxNum + 1 existing = PREFIX & Format(maxNum, "0000") End If ws.Cells(r, "A").Value = existing End If Next r End Sub

3
Run the macro

Return to the worksheet you want to update. Select Tools > Macro > Macros, choose AssignSerialNumbers, and click Run.

4
Check and save the results

Review column A, including any preexisting identifiers. Use File > Save As and select an Excel Macro-Enabled Workbook (.xlsm) if you want to retain the macro.

Use VBA to assign and reuse sequential identifiers
Existing conflicts and future rows: The macro does not change existing identifiers or resolve conflicting IDs already assigned to the same pair. Run it again after adding rows; it does not update automatically.
Work with serial numbers in WPS Spreadsheets

Assign serial numbers with a VBA-enabled WPS Spreadsheets installation

In a WPS Spreadsheets desktop installation that supports VBA, use the macro above to reuse identifiers for matching pairs and assign new sequential numbers. VBA availability depends on your WPS edition and platform.

  1. 1. Check VBA availability: Open WPS Spreadsheets and check the Developer tab for Visual Basic or Macros. Confirm that your WPS edition and platform support VBA before proceeding.
  2. 2. Open the workbook: Use Menu > Open to open the spreadsheet, then select the worksheet containing columns A, B, and C.
  3. 3. Add the macro: Open Developer > Visual Basic, insert a module, and paste the AssignSerialNumbers code from the solution above.
  4. 4. Run and save: Select Developer > Macros, run AssignSerialNumbers, and review column A. Use Save As to save the workbook in XLSM format if you need to retain the macro.
Reuse serial numbers for matching name and information pairsOpen and save Microsoft Excel formats, including XLSX and XLSMWork in a familiar spreadsheet interface
microsoft office alternative - wps office

Frequently Asked Questions

Will the macro overwrite serial numbers already in column A?

No. It fills only blank cells in column A. Review existing entries manually if the same pair already has conflicting identifiers.

How do I change the serial number prefix?

In the VBA editor, change the PREFIX constant from "SN-" to your desired prefix. The macro calculates the next number from existing IDs that use that prefix followed by digits.

What happens when a name or information cell is blank?

The macro skips the row. Fill both columns B and C, then run AssignSerialNumbers again from Tools > Macro > Macros.

Does the macro assign numbers automatically when I add a row?

No. The example is a manually run macro. After adding rows, select Tools > Macro > Macros and run AssignSerialNumbers again.

Can I use the macro in every version of WPS Spreadsheets?

No. VBA support depends on the WPS edition and platform. Check for Visual Basic and Macros on the Developer tab and confirm VBA support before using the code.