Generate workbook unique ID from template in Excel

3.2k views Asked by At

In my organization we have an Excel template that all employees have to fill frequently. This template originates hundreds/thousands of Excel files (workbooks) per year.

For the sake of organisation, I urgently need to have a unique ID for each of these files (i.e. unique ID per workbook generated by this template).

Currently, my idea is to generate the following ID in a cell of the workbook:

[user]-[YYYYMMDD]-[hhmmss]

in which:

  • user is a string representing the username of the employee which would be filled in by the user. So no problem here.
  • YYYYMMDD is year, month and day concatenated
  • hhmmss is hour, minute and second concatenated

For this effect, I would need that my Excel template automatically fills a cell with the YYYYMMDD-hhmmss information with the exact date and time of generation.

This information should be created once the template generates the workbook, and cannot be changed ever after. So these should be values in a (protected) cell and not a formula (I guess).

I cannot figure out how to do this after searching for a long time. I am not sure if it is needed or not, but I am no master of VBA.

2

There are 2 answers

1
MikeD On BEST ANSWER

The idea of having a date/time field is good .... create a workbook smilar to this

enter image description here

add the following code to the ThisWorkbook module:

Private Sub Workbook_Open()
    If [B2] = "" Then
        ' timestamp
        [B2] = Now()

        ' suppress warning when saving macro containing workbook in non-macro format
        Application.DisplayAlerts = False

        ' save under calculated name
        ActiveWorkbook.SaveAs [B1] & "-" & Format([B2], "YYYYMMDD-hhmmss")

        ' turn on alerts again
        Application.DisplayAlerts = True
    End If
End Sub

and save as a macro enabled template

Then create a [File - New] from this template .... it will immediately be saved under the name of the user with macros removed so that the code can't hit it another time.

The user name could be retrived from the environment or from the registry.

Alternatively you can examine if the file has a true name or (still) is named Book nnn which means it hasn't been saved before; this removes the need to reserve a timestamp cell in your workbook

0
ChrisProsser On

Here are a couple of functions you could use to get your id. If you put this inside a vba module on your template you will be able to call the functions from the worksheets or other vba code (e.g. in workbook just enter '=get_id()', in vba you would do something like 'id = get_id()' to call this:

Option Explicit

Public Function lpad(string1, padded_length, _
                     Optional pad_string = " ")
    Dim chars

    chars = padded_length - Len(string1)
    lpad = WorksheetFunction.Rept(pad_string, chars) & string1

End Function

Public Function get_id()
    Dim user
    Dim YYYYMMDD
    Dim hhmmss

    user = Environ("username")
    YYYYMMDD = Year(Now()) & lpad(Month(Now()), 2, 0) & lpad(Day(Now()), 2, 0)
    hhmmss = lpad(Hour(Now()), 2, 0) & lpad(Minute(Now()), 2, 0) & lpad(Second(Now()), 2, 0)

    get_id = user & "-" & YYYYMMDD & "-" & hhmmss

End Function

The lpad function is just for formatting (so that you get 07 for July instead of 7 etc.). I have also added something here to set the user to the windows environment variable for the current user's name, but if you want to promt the user instead this part could easily be replaced.

Let me know if you have any questions.