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:
Figure 1. The New Name dialog box.
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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11192) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Generating Unique Numbers for Worksheets.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Does your macro need to know how many windows Excel has open? You can determine it by using the Count property of the ...
Discover MoreWrite out a check and you need to include the digits for the amount of the check and the value of the check written out ...
Discover MoreWant your macros to be available regardless of the workbook on which you are working? Here's how to store them in the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-04-06 01:59:59
Mufz
Can we execute the last macro upon clicking of a button?
2019-04-05 11:46:13
Dave Bonin
Slight correction...
The second-last formula I provided should be:
= TEXT( ROUND( NOW() - DATE( 2019, 1, 1 ), 2 ), "0000.00" )
We need to round to two digits, not three as I first wrote.
The number of digits to round to should match the TEXT() format.
2019-04-04 12:31:13
Dave Bonin
Ok, let's try something a little more human-scale...
By that I mean:
- Let's have enough digits to create unique numbers.
- But not so many that they're hard to remember.
- Let's use a format that naturally breaks the number into easy pieces.
- And let's take advantage of there being 1440 seconds in a day which is pretty close to 1000.
So, if you're going to generate (and then copy) a number, you could use:
= TEXT( ROUND( NOW() - DATE( 2019, 1, 1 ), 3 ), "0000.000" )
This formula says:
- We're going to start generating numbers on January 1, 2019. No need to create any numbers for before that date.
- We assume that we don't need new numbers any faster than one every 1.440 seconds.
- And we aren't worried about repeating any numbers for at least 9999 / 365 = 27.4 years.
I just ran this formula a few moments ago and it generated the number 0093.472
That seems to be easy enough to remember.
If you won't generate new numbers any faster than one per 14.40 seconds, then we can shave off a digit like so:
= TEXT( ROUND( NOW() - DATE( 2019, 1, 1 ), 3 ), "0000.00" ) <OR>
= TEXT( ROUND(( NOW() - DATE( 2019, 1, 1 )) / 10, 3 ), "000.000" )
These two formulas generate 0093.47 and 009.347, respectively, and both formulas are good for 27.4 years before repeating.
REMEMBER that you need to copy this value when you first start to use it as the formula will keep changing it every time your workbook recalculates.
2016-09-02 08:09:31
Tajsia Oakley
Is there a way these instructions can be broken down any simpler?
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments