Hello drdavidge,
This macro is a UDT (User Defined Funfction). This can be used on a worksheet or in VBA code. It produces a Globally Unique IDentifier string.
According to Microsoft...
A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.
From Wikipedia...
While each generated GUID is not guaranteed to be unique, the total number of unique keys (2^128 or 3.4×10^38) is so large that the probability of the same number being generated twice is very small. For example, consider the observable universe, which contains about 5×10^22 stars; every star could then have 6.8×10^15 universally unique GUIDs.
Macro Code
Copy this code into a VBA standard module.
'Written: January 24, 2010
'Author: Leith Ross
'Summary: Creates a Globally Unique Identifier (GUID) string.
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Declare Function CoCreateGuid _
Lib "OLE32.dll" (ByRef pGuid As GUID) As Long
Public Function GetGUID() As String
Dim I As Integer
Dim myGUID As GUID
Dim S As String
If (CoCreateGuid(myGUID) = 0) Then
S = _
String(8 - Len(Hex$(myGUID.Data1)), "0") & Hex$(myGUID.Data1) & "-" & _
String(4 - Len(Hex$(myGUID.Data2)), "0") & Hex$(myGUID.Data2) & "-" & _
String(4 - Len(Hex$(myGUID.Data3)), "0") & Hex$(myGUID.Data3) & "-"
For I = 0 To 7
S = S & IIf(myGUID.Data4(I) < &H10, "0", "") & Hex(myGUID.Data4(I))
If I = 1 Then S = S & "-"
Next I
GetGUID = S
End If
End Function
VBA Usage Example
Running this code in VBA will produce a GUID and store it in the string variable S
Sub GUIDTest()
Dim S As String
S = GetGUID
End Sub
Worksheet Example
Place the formula =GetGUID() in a cell. The cell will hold a GUID.
Bookmarks