# Generating Unique Numbers for Worksheets

Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Generating Unique Numbers for Worksheets.

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. Choose Name from the Insert menu, then choose Define. Excel displays the Define Name dialog box. (See Figure 1.)
2. Figure 1. The Define Name dialog box.

3. In the Name box, enter a name such as MaxNum.
4. 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.
5. 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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3336) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Generating Unique Numbers for Worksheets.

Related Tips:

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!

 *Name: Email: Notify me about new comments ONLY FOR THIS TIP Notify me about new comments ANYWHERE ON THIS SITE Hide my email address *Text: *What is 5+3 (To prevent automated submissions and spam.)

John    10 Jan 2012, 05:49
Thanks, Alan!
awyatt    09 Jan 2012, 10:18
Try here, John: http://excel.tips.net/T001951_Referencing_Worksheet_Tabs.html
John Rose    07 Jan 2012, 09:00
What if the outcome required were not the identifier of the worksheet in cell A1 but as the name on the tab (Possibly more useful?

# Our Company

Sharon Parq Associates, Inc.

# Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Excel Products

Word Products

# Our Authors

Author Index

Write for Tips.Net