Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Generating Unique Numbers for Worksheets.

Generating Unique Numbers for Worksheets

Written by Allen Wyatt (last updated February 26, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


1

Sometimes you may need Excel to generate a unique number for your worksheets. For instance, you could be using Excel to create forms such as invoices, statements, or tracking sheets, and you need unique numbers for each form (I'll call this a ticket number). This, of course, implies that Excel needs to remember the number from one session to the next.

There are a couple of ways you can approach this problem. If the numbers don't need to be sequential, you could create a ticket number based on the current time of day, in seconds. The following macro can be added to the ThisWorksheet object:

Private Sub Workbook_NewSheet(ByVal Sh As Object)
    Dim lTicket As Long
    lTicket = CLng(Time * 24 * 60 * 60)
    Sh.Range("A1") = lTicket
End Sub

The macro is triggered every time a new worksheet is added to the workbook. It takes the current time, converts it to an integer number of seconds, and then places that value into cell A1. The likelihood of duplicating ticket numbers within any given day is remote, but it could happen over time. (For instance, if you create a ticket at the exact same time today that you did yesterday or last week.)

To get around this problem, you could create a ticket number in the following manner:

Private Sub Workbook_NewSheet(ByVal Sh As Object)
    Dim sTemp As String
    sTemp = Format(Date, "yymmdd") & Format(Time, "hhmmss")
    Sh.Range("A1") = sTemp
End Sub

This version of the event handler constructs a ticket number based both the date and time. Unless you are creating tickets very quickly, this approach should reduce the possibility of duplicate numbers generated by the macro.

If the numbers must be sequential within the current workbook, then you can define a name that contains the current high value of your ticket number, and then a macro that places that number in a cell on a new worksheet and increments the value of the stored number. Follow these steps to start:

  1. Display the Formulas tab of the ribbon.
  2. Click on the Define Name tool in the Defined Names group. Excel displays the New Name dialog box. (See Figure 1.)
  3. Figure 1. The New Name dialog box.

  4. In the Name box, enter a name such as MaxNum.
  5. In the Refers To area at the bottom of the dialog box, enter an equal sign followed by the value you want used for the next ticket number.
  6. Click on OK. The new name is stored in the workbook.

Now, add the following macro to the ThisWorksheet object in the VBA Editor:

Private Sub Workbook_NewSheet(ByVal Sh As Object)
    Dim iMax As Integer
    iMax = Mid(ThisWorkbook.Names("MaxNum"), 2)
    Sh.Range("A1") = iMax
    iMax = iMax + 1
    ThisWorkbook.Names("MaxNum").RefersTo = "=" & iMax
End Sub

This macro is executed every time you insert a new worksheet in the workbook. It retrieves the value you stored in the MaxNum, places that value into cell A1 of the new worksheet, and then increments what is stored in MaxNum.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11192) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Generating Unique Numbers for Worksheets.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Backing Up Label Layouts

Once you create a custom label layout it is a good idea to backup the layout on a different hard drive in the event of a ...

Discover More

Setting the Left Indent of a Paragraph in a Macro

When using a macro to format text, you can set all sorts of attributes for paragraphs or individual characters. On ...

Discover More

Inserting Hyperlinks

Connect your worksheets with other workbooks or with the world of the Internet. The ability to add hyperlinks makes this ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Deriving an Absolute Value in a Macro

Need to figure out an absolute value within your macro code? It's easy to do using the Abs function, described in this tip.

Discover More

Macro Runs Slowly, but Steps Quickly

When you have a macro that processes a huge amount of data, it can seem like it takes forever to finish up. These ...

Discover More

Creating a String in a Macro

Need to put together a bunch of characters to create a text string? You can do it in your macros by using the String ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five more than 3?

2022-02-26 12:31:17

J. Woolley

My Excel Toolbox includes the VBA support function GUID_String(). The probability that any single result will be duplicated is negligible.
See https://en.wikipedia.org/wiki/Universally_unique_identifier
See http://www.cpearson.com/Excel/CreateGUID.aspx
See https://sites.google.com/view/MyExcelToolbox/


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.