How to Automatically Generate the Next Document Number in Excel VBA
Question details
The user needs to automatically populate a UserForm text control (TxtDocNum) with the next sequential document number based on existing numerical records in a specific worksheet column.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Initializing an Excel VBA UserForm designed for data entry, which requires assigning a unique, auto-incremented document number upon opening.
- Observed behavior
- The form needs logic in its initialization event to scan column X on Sheet1, identify the highest existing document number, add 1 to it, and display the result in the TxtDocNum field.
Ensure that your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled Developer tools on your ribbon to access the VBA editor.
Use WorksheetFunction.Max to Find the Highest Number
Calculate the next document number reliably by finding the maximum numeric value currently stored in your designated column and adding one.
This method is highly recommended because it finds the largest number regardless of how the rows are sorted. Even if a user deletes a row or sorts the data differently, WorksheetFunction.Max ensures you always get the correct next sequence number.
Press ALT + F11 to launch the Visual Basic Editor, then locate and double-click your UserForm in the Project Explorer panel.
Right-click the UserForm background, select 'View Code', and choose the 'UserForm_Initialize' event from the top-right dropdown menu.
Inside the Private Sub UserForm_Initialize(), type the following to calculate the maximum value in Column X on Sheet1: LastDocNum = Application.WorksheetFunction.Max(Sheets("Sheet1").Range("X:X"))
On the next line, assign the calculated next number to your text box control by typing: TxtDocNum = LastDocNum + 1

Reference the Last Row's Value Directly
If your document numbers are strictly sequential, sorted, and have no gaps, you can find the very last populated row and increment its specific value.
Create and Run VBA Macros Easily with WPS Spreadsheet
WPS Spreadsheet provides comprehensive support for VBA macros, allowing you to automate repetitive tasks like generating sequential document numbers in your forms without needing expensive software.
- 1. Download and Install WPS Office: Download WPS Office and open your macro-enabled workbook (.xlsm) in WPS Spreadsheet.
- 2. Enable Developer Tools: Go to the 'Developer' tab on the top ribbon and click 'VBA Editor' to launch the scripting environment.
- 3. Implement the UserForm Code: Insert or edit your UserForm and apply the WorksheetFunction.Max script within the UserForm_Initialize event.
- 4. Run and Automate: Save your workbook and run the macro to automatically populate your document numbers seamlessly.

Frequently Asked Questions
How do I handle alphanumeric document numbers in VBA?
If your document numbers include text (e.g., 'INV-001'), WorksheetFunction.Max will not work directly since it ignores text strings. You will need to extract the numeric portion using string functions like Right() or Mid(), increment the number, format it, and concatenate the text prefix back on.
Why is my VBA UserForm control returning empty or zero?
This usually happens if the sheet name or range referenced in your VBA code does not match your actual workbook layout. Double-check that 'Sheet1' and 'X:X' accurately reflect the exact spelling of your worksheet tab and the column where your data is stored.
Can I generate the next document number without using a UserForm?
Yes, you can trigger the generation of a new document number using a standard module macro attached to a worksheet button, or automatically via worksheet events like Worksheet_Change, depending on your preferred data entry workflow.
Why does my code return the row number instead of the document number?
If you are referencing a cell using the LastRow method but forget to append '.Value' at the end of the Cells() object, VBA might improperly evaluate the object references. Always explicitly use .Value (e.g., Cells(LastRow, 24).Value) when retrieving stored data.




