Search code examples
excelexcel-2007publicuniqueidentifiervba

Creating Unique ID Based on Date, and Number of Requests


I have created a spreadsheet (Excel 2007) that will track orders over the course of a year. As part of that tracking, I need to generate a unique identifier based on the date, and the number of orders that arrive in a day. The count resets each day.

For example;

Request 1= Date: 02/02/16 -> ID # 20160202-01

Request 2 = Date: 02/02/16 -> ID # 20160202-02

Request 3 = Date: 02/03/16 -> ID # 20160202-01

Due to the nature of the business, orders arrive in e-mail format, and are then entered into the spreadsheet via a userform. Within this userform, I have tried an If..Then statement to increment a variable (n) if the date of the request matches the previous entry. This works, but the value of (n) resets whenever the form is ran, meaning that my maximum value I can get is 2.

I believe the fix lies in making (n) a global/public variable, but I do not know how to use a public variable to be accessed via a userform. Does anyone have any advice, or good references to fix this problem? Thank you in advance for your help.

UPDATE: Thank you everyone for the quick feedback. I just had a colleague stop by, and on a whim, asked her if she had a fix for me. She informed me that a Static variable would be the way to fix this problem. This fix helped, but is not a perfect solution. I'll post the relevant code for this to show the implemented fix.

Sub CommandButtonSubmitClose_Click()
 Static n As Integer
 Dim ordDate As Date
 Dim ordYear As Integer
 Dim txtYear As String
 Dim txtMonth As String
 Dim txtDay As String
 Dim txtCount As String
 Dim IDnum As String
 Dim prevRow As Long
 Dim LastRow As Long, ws As Worksheet

 'Define variables'
 prevRow = LastRow - 1
 txtYear = reqYear
 txtMonth = Format(month(reqDate), "00")
 txtDay = Format(day(reqDate), "00")  

If ordDate = ws.Range("A" & prevRow).Value Then
 n = n + 1
 Else                    'Determine daily count'
   n = 1
 End If


txtCount = Format(n, "00")

'Create ID Number'

  IDnum = " " & txtYear & "" & txtMonth & "" & txtDay & "-" & txtCount & ""

New Issues: As stated in the comments, the variable is not stored if the spreadsheet is closed. So if orders come in later in the day, the count is reset, creating duplicates that cause the whole thing to go haywire. Also, if there is a non-consecutive date in the mix, the generator does not work.


Solution

  • After experimenting with some of the ideas suggested, including storing the count value in a cell and another sheet, I settled on one that instead uses a loop to search for orders that are received on the same day. using a For...Next loop, I ran through each order, and if the date matched the date of the order, the count (n) was updated by one. Once the loop had played through, (n) was used as above. If no other values are found, (n) is set to 1. The value of (n) resets to zero before the loop. Thank you for the feedback everyone, I appreciate the help!

    Here is the code of the fix if anyone is interested:

    n = 1
    For i = 11 To LastRow
        If ordDate = ws.Range("A" & i).Value Then
         n = n + 1
        End If
     Next i
    
     txtCount = Format(n, "00")
    
    'Create ID Number'
    
    IDnum = " " & txtYear & "" & txtMonth & "" & txtDay & "-" & txtCount & ""