79

I need a function to add a GUID to cells in excel. I found this previous question on stackoverflow, but it is not working. It suggests the following function:

=CONCATENATE(DEC2HEX(RANDBETWEEN(0,4294967295),8),"-",DEC2HEX(RANDBETWEEN(0,6553‌​5),4),"-"
    ,DEC2HEX(RANDBETWEEN(16384,20479),4),"-",DEC2HEX(RANDBETWEEN(32768,49151‌​),4),"-"
    ,DEC2HEX(RANDBETWEEN(0,65535),4),DEC2HEX(RANDBETWEEN(0,4294967295),8))

I have been unable to get the concatenate method to work, and so tried it using the "&" figure to concatenate anyway. That seemed to work, but then I get an inexplicable error on the second DEX2HEX block: DEC2HEX(RANDBETWEEN(0,6553‌​5),4). The Excel formula evaluator says it's invalid, but I can't seem to figure out why at all. Any ideas?

11
  • Why not just use the VBA code supplied in the same question? Commented Feb 20, 2013 at 21:30
  • Which version of Excel are you using? AFAIK RANDBETWEEN was only introduced in 2007. If you use 2003, replace it with INT(RAND()*65535) Commented Feb 20, 2013 at 21:32
  • @PeterAlbert but then I get an inexplicable error on the second DEX2HEX block - assuming that 1st block is ok)
    – Peter L.
    Commented Feb 20, 2013 at 21:33
  • 1
    @LittleBobbyTables tried that, it didnt' seem to work either. The function just produced a #!VALUE error.
    – fraxture
    Commented Feb 20, 2013 at 22:16
  • 1
    I suppose I don't have to point out that this doesn't generate "real" GUIDs - because they have no version number. See en.wikipedia.org/wiki/Globally_unique_identifier for details
    – Fowl
    Commented Apr 13, 2016 at 6:03

15 Answers 15

116

As of modern version of Excel, there's the syntax with commas, not semicolons. I'm posting this answer for convenience of others so they don't have to replace the strings- We're all lazy... hrmp... human, right?

=CONCATENATE(DEC2HEX(RANDBETWEEN(0,4294967295),8),"-",DEC2HEX(RANDBETWEEN(0,65535),4),"-",DEC2HEX(RANDBETWEEN(0,65535),4),"-",DEC2HEX(RANDBETWEEN(0,65535),4),"-",DEC2HEX(RANDBETWEEN(0,4294967295),8),DEC2HEX(RANDBETWEEN(0,65535),4))

Or, if you like me dislike when a guid screams and shouts and you, we can go lower-cased like this.

=LOWER(CONCATENATE(DEC2HEX(RANDBETWEEN(0,4294967295),8),"-",DEC2HEX(RANDBETWEEN(0,65535),4),"-",DEC2HEX(RANDBETWEEN(0,65535),4),"-",DEC2HEX(RANDBETWEEN(0,65535),4),"-",DEC2HEX(RANDBETWEEN(0,4294967295),8),DEC2HEX(RANDBETWEEN(0,65535),4)))

17
  • 13
    Comas and semicolons differs because of localization but not because of modern or not so modern Excel. However good answer
    – trigras
    Commented Dec 8, 2017 at 8:40
  • 11
    I needed semicolons, i.e. =CONCATENATE(DEC2HEX(RANDBETWEEN(0;4294967295);8);"-";DEC2HEX(RANDBETWEEN(0;42949);4);"-";DEC2HEX(RANDBETWEEN(0;42949);4);"-";DEC2HEX(RANDBETWEEN(0;42949);4);"-";DEC2HEX(RANDBETWEEN(0;4294967295);8);DEC2HEX(RANDBETWEEN(0;42949);4))
    – Matt
    Commented Aug 15, 2019 at 13:54
  • 5
    If believe RANDBETWEEN(0,42949) should be RANDBETWEEN(0,65535), otherwise the four digit parts of the GUID will only be between 0000 and A7C5 , whereas they should go all the way to FFFF Commented Oct 26, 2021 at 11:46
  • 3
    @ReversedEngineer You are correct. I'm impressed partly because you noticed it and partly because no one else did in almost 5 years! I believe I started with 4294967295, which corresponds to 16^8, i.e. 8 consecutive F. Then, I picked the 4 first initial, decimal digits. My bad. Commented Oct 27, 2021 at 5:19
  • 3
    Note that this is not UUID v4. The 3rd group needs to start with 4. Commented Feb 24, 2023 at 19:35
41
=CONCATENATE(
    DEC2HEX(RANDBETWEEN(0;4294967295);8);"-";
    DEC2HEX(RANDBETWEEN(0;42949);4);"-";
    DEC2HEX(RANDBETWEEN(0;42949);4);"-";
    DEC2HEX(RANDBETWEEN(0;42949);4);"-";
    DEC2HEX(RANDBETWEEN(0;4294967295);8);
    DEC2HEX(RANDBETWEEN(0;42949);4)
)
5
  • 1
    Using Excel 2013 I had to replace all semi-colons with commas.
    – biscuit314
    Commented Oct 25, 2016 at 15:50
  • 10
    I needed to replace the ";" with "," and then everything worked out great! Thanks :) '=CONCATENATE(DEC2HEX(RANDBETWEEN(0,4294967295),8),"-",DEC2HEX(RANDBETWEEN(0,42949),4),"-",DEC2HEX(RANDBETWEEN(0,42949),4),"-",DEC2HEX(RANDBETWEEN(0,42949),4),"-",DEC2HEX(RANDBETWEEN(0,4294967295),8),DEC2HEX(RANDBETWEEN(0,42949),4))' Commented Dec 7, 2016 at 19:38
  • 7
    If it walks like a GUID and talks like a GUID it's not a GUID.
    – IvanP
    Commented May 11, 2017 at 7:40
  • 2
    Please note that this doesn't generate a valid version 4 GUID/UUID, see en.wikipedia.org/wiki/… The formula in the question is perfectly fine (stackoverflow.com/a/12219726/729642), or look at the answers from Masud, Fredder, or nidkil. Commented May 24, 2019 at 15:50
  • If believe RANDBETWEEN(0;42949) should be RANDBETWEEN(0;65535), otherwise the four digit parts of the GUID will only be between 0000 and A7C5 , whereas they should go all the way to FFFF Commented Oct 26, 2021 at 11:45
38

I am using the following function in v.2013 excel vba macro code

Public Function GetGUID() As String 
    GetGUID = Mid$(CreateObject("Scriptlet.TypeLib").GUID, 2, 36) 
End Function 
5
  • couldnt get PeterL's solution to work in Excel 2013.. and after a total of 10 seconds of debugging the function, I found your solution to be a better fit for me. Works perfect. thanks! Commented Aug 21, 2014 at 21:22
  • 11
    This should be used instead of Frazture's answer because it is much more straightforward and relies on the system to generate the guid. Fraxture's solution (no offense) does not even create a valid guid as it doesn't take things such as the current time, MAC address, etc. into account. They also won't include the guid version bit. Guids are not just a set of random characters, there is meaning behind them. More info here: en.wikipedia.org/wiki/Globally_unique_identifier#Algorithm
    – Josh M.
    Commented Apr 12, 2016 at 15:37
  • 4
    Attention: there may be problem with the library (Permission denied) after installing win update: stackoverflow.com/questions/45082258/…
    – Radek
    Commented Jul 20, 2017 at 11:17
  • 2
    I confirm @Radek 's comment that after a security update to Office, this mechanism no longer works. stackoverflow.com/a/45332789/575559 documents the Microsoft recommended methods to prevent the above function from causing a "permission denied" error OR to make use of an alternative function.
    – DeChrist
    Commented Sep 20, 2017 at 10:55
  • Declare Function CoCreateGuid Lib "ole32" (ByRef GUID As Byte) As Long Public Function GenerateGUID() As String Dim ID(0 To 15) As Byte Dim N As Long Dim GUID As String Dim Res As Long Res = CoCreateGuid(ID(0)) For N = 0 To 15 GUID = GUID & IIf(ID(N) < 16, "0", "") & Hex$(ID(N)) If Len(GUID) = 8 Or Len(GUID) = 13 Or Len(GUID) = 18 Or Len(GUID) = 23 Then GUID = GUID & "-" End If Next N GenerateGUID = GUID End Function
    – rchacko
    Commented Aug 23, 2018 at 4:06
19
=LOWER(
    CONCATENATE(
        DEC2HEX(RANDBETWEEN(0,POWER(16,8)),8), "-", 
        DEC2HEX(RANDBETWEEN(0,POWER(16,4)),4),"-","4", 
        DEC2HEX(RANDBETWEEN(0,POWER(16,3)),3),"-",
        DEC2HEX(RANDBETWEEN(8,11)),
        DEC2HEX(RANDBETWEEN(0,POWER(16,3)),3),"-",
        DEC2HEX(RANDBETWEEN(0,POWER(16,8)),8),
        DEC2HEX(RANDBETWEEN(0,POWER(16,4)),4)
    )
)

Taken from git @mobilitymaster.

7
  • 1
    I pasted this once into a cell and it seemed to work well. But then I pasted it into another cell and the first cell changed. Then I tried another cell and the first 2 cells changed... why does it do that? Commented Nov 27, 2017 at 15:43
  • 2
    It is due to the Automatic calculation setting on in excel. If you go into Excel Options, then Formulas, then you will see something called "Workbook Calcuation" if you disable it, it will only run the calculations when you manually press the button in the bottom pane. Commented Nov 5, 2018 at 23:04
  • 1
    WARNING: This has a random constant "4" in the middle of the formula...
    – Sancarn
    Commented Dec 18, 2018 at 15:16
  • 2
    @Sancarn: It generates a valid version 4 GUID/UUID, see en.wikipedia.org/wiki/… (Also note the other 2 fixed bits which are forced by using RANDBETWEEN(8,11)) Commented May 24, 2019 at 15:48
  • 2
    @Sancarn: That's how GUID/UUIDs are defined. In the different versions all those bits and bytes have different meanings (e.g. version 3 and 5 are based on namespace hashes). Only Version 4 is meant to be random, except those couple version and variant bits to distinguish them from the other versions/variants.                                                                            The Wikipedia article also has en entry about collisions, which is probably correct :-), stating for version 4: "... the probability to find a duplicate within 103 trillion version-4 UUIDs is one in a billion." Commented Jun 10, 2019 at 22:42
10

This is not a problem with the function at all.

It took me a bit of digging, but the problem is in copying and pasting. Try copying this: RANDBETWEEN(0,6553‌​5) string, posted in your original question, and paste it into a Hex Editor, then you'll see that there are actually two null characters in the 65535:

00000000  52 41 4E 44 42 45 54 57 45 45 4E 28 30 2C 36 35  RANDBETWEEN(0,65
00000010  35 33 00 00 35 29                                53‌..​5)
2
  • 2
    you are the true hacker and genius here
    – hongsy
    Commented Jul 12, 2017 at 9:25
  • @hongsy: Thank-you! True appreciation ;o)
    – user1945782
    Commented Jul 12, 2017 at 12:35
3

for me it is correct, in Excel spanish

=CONCATENAR(
DEC.A.HEX(ALEATORIO.ENTRE(0,4294967295),8),"-",
DEC.A.HEX(ALEATORIO.ENTRE(0,65535),4),"-",
DEC.A.HEX(ALEATORIO.ENTRE(16384,20479),4),"-",
DEC.A.HEX(ALEATORIO.ENTRE(32768,49151),4),"-",
DEC.A.HEX(ALEATORIO.ENTRE(0,65535),4),
DEC.A.HEX(ALEATORIO.ENTRE(0,4294967295),8)
)
1

The formula for Dutch Excel:

=KLEINE.LETTERS(
    TEKST.SAMENVOEGEN(
        DEC.N.HEX(ASELECTTUSSEN(0;MACHT(16;8));8);"-";
        DEC.N.HEX(ASELECTTUSSEN(0;MACHT(16;4));4);"-";"4";
        DEC.N.HEX(ASELECTTUSSEN(0;MACHT(16;3));3);"-";
        DEC.N.HEX(ASELECTTUSSEN(8;11));
        DEC.N.HEX(ASELECTTUSSEN(0;MACHT(16;3));3);"-";
        DEC.N.HEX(ASELECTTUSSEN(0;MACHT(16;8));8);
        DEC.N.HEX(ASELECTTUSSEN(0;MACHT(16;4));4)
    )
)
0

The formula for French Excel:

=CONCATENER(
DECHEX(ALEA.ENTRE.BORNES(0;4294967295);8);"-";
DECHEX(ALEA.ENTRE.BORNES(0;42949);4);"-";
DECHEX(ALEA.ENTRE.BORNES(0;42949);4);"-";
DECHEX(ALEA.ENTRE.BORNES(0;42949);4);"-";
DECHEX(ALEA.ENTRE.BORNES(0;4294967295);8);
DECHEX(ALEA.ENTRE.BORNES(0;42949);4))

As noted by Josh M, this does not provide a compliant GUID however, but this works well for my current need.

1
  • 1
    For a compliant one see the formula in the actual question, or look at the answers from Masud, Fredder, or nidkil. Commented May 24, 2019 at 15:52
0

The formula for German Excel:

=KLEIN(
    VERKETTEN(
        DEZINHEX(ZUFALLSBEREICH(0;POTENZ(16;8));8);"-";
        DEZINHEX(ZUFALLSBEREICH(0;POTENZ(16;4));4);"-";"4";
        DEZINHEX(ZUFALLSBEREICH(0;POTENZ(16;3));3);"-";
        DEZINHEX(ZUFALLSBEREICH(8;11));
        DEZINHEX(ZUFALLSBEREICH(0;POTENZ(16;3));3);"-";
        DEZINHEX(ZUFALLSBEREICH(0;POTENZ(16;8));8);
        DEZINHEX(ZUFALLSBEREICH(0;POTENZ(16;4));
    )
)
1
  • You may be missing closing brackets ) on this one.
    – user1945782
    Commented Oct 10, 2018 at 7:03
0

After trying a number of options and running into various issue with newer versions of Excel (2016) I came across this post from MS that worked like a charm. I enhanced it bit using some code from a post by danwagner.co

Private Declare PtrSafe Function CoCreateGuid Lib "ole32.dll" (Guid As GUID_TYPE) As LongPtr

Private Declare PtrSafe Function StringFromGUID2 Lib "ole32.dll" (Guid As GUID_TYPE, ByVal lpStrGuid As LongPtr, ByVal cbMax As Long) As LongPtr

Function CreateGuidString(Optional IncludeHyphens As Boolean = True, Optional IncludeBraces As Boolean = False)
    Dim Guid As GUID_TYPE
    Dim strGuid As String
    Dim retValue As LongPtr

    Const guidLength As Long = 39 'registry GUID format with null terminator {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

    retValue = CoCreateGuid(Guid)

    If retValue = 0 Then
        strGuid = String$(guidLength, vbNullChar)
        retValue = StringFromGUID2(Guid, StrPtr(strGuid), guidLength)

        If retValue = guidLength Then
            '   valid GUID as a string
            '   remove them from the GUID
            If Not IncludeHyphens Then
                strGuid = Replace(strGuid, "-", vbNullString, Compare:=vbTextCompare)
            End If

            '   If IncludeBraces is switched from the default False to True,
            '   leave those curly braces be!
            If Not IncludeBraces Then
                strGuid = Replace(strGuid, "{", vbNullString, Compare:=vbTextCompare)
                strGuid = Replace(strGuid, "}", vbNullString, Compare:=vbTextCompare)
            End If


            CreateGuidString = strGuid
        End If
    End If

End Function


Public Sub TestCreateGUID()
    Dim Guid As String
    Guid = CreateGuidString() '<~ default
    Debug.Print Guid
End Sub

There are additional options in the original MS post found here: https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-msoffice_custom-mso_2010/guid-run-time-error-70-permission-denied/c9ee4076-98af-4032-bc87-40ad7aa7cb38

2
  • If I try this I get the error: User defined type not declared (Windows 10, Excel 2019)
    – habakuk
    Commented Jul 2, 2020 at 9:46
  • @habakuk This needs the definition for GUID_TYPE from the previous answer.
    – Craig
    Commented Apr 19, 2021 at 18:48
0

Ken Thompson is right! - for me also this way works (excel 2016), but type definition GUID_TYPE is skipped, so full scripting is:

Private Type GUID_TYPE
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(7) As Byte
End Type

Private Declare PtrSafe Function CoCreateGuid Lib "ole32.dll" (Guid As GUID_TYPE) As LongPtr

Private Declare PtrSafe Function StringFromGUID2 Lib "ole32.dll" (Guid As GUID_TYPE, ByVal lpStrGuid As LongPtr, ByVal cbMax As Long) As LongPtr

Function CreateGuidString(Optional IncludeHyphens As Boolean = True, Optional IncludeBraces As Boolean = False)
    Dim Guid As GUID_TYPE
    Dim strGuid As String
    Dim retValue As LongPtr

    Const guidLength As Long = 39 'registry GUID format with null terminator {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

    retValue = CoCreateGuid(Guid)

    If retValue = 0 Then
        strGuid = String$(guidLength, vbNullChar)
        retValue = StringFromGUID2(Guid, StrPtr(strGuid), guidLength)

        If retValue = guidLength Then
            '   valid GUID as a string
            '   remove them from the GUID
            If Not IncludeHyphens Then
                strGuid = Replace(strGuid, "-", vbNullString, Compare:=vbTextCompare)
            End If

            '   If IncludeBraces is switched from the default False to True,
            '   leave those curly braces be!
            If Not IncludeBraces Then
                strGuid = Replace(strGuid, "{", vbNullString, Compare:=vbTextCompare)
                strGuid = Replace(strGuid, "}", vbNullString, Compare:=vbTextCompare)
            End If


            CreateGuidString = strGuid
        End If
    End If

End Function
0

In addition to all the other valuable answers here. For those interested in a universally unique identifier (UUID) version 4 which complies with RFC 4122, I suggest this formula.

=CONCATENATE(
DEC2HEX(RANDBETWEEN(0;4294967296);8);"-";
DEC2HEX(RANDBETWEEN(0;65536);4);"-";
DEC2HEX(RANDBETWEEN(0;65536);4);"-4";
DEC2HEX(RANDBETWEEN(0;4096);3);"-";
DEC2HEX(RANDBETWEEN(0;4294967296);8);
DEC2HEX(RANDBETWEEN(0;65536);4))

Example of random end result 8D8AC610-566D-4EF0-9C22-186B2A5ED793


Below is the answer as above. But with details for those interested.

Assumptions about the formula above:

If your MS Excel or LibreOffice is in another local (language), you need to adapt this formula appropriately.

For example, in French the correct formula is this:

=CONCATENER(
DECHEX(ALEA.ENTRE.BORNES(0;4294967296);8);"-";
DECHEX(ALEA.ENTRE.BORNES(0;65536);4);"-";
DECHEX(ALEA.ENTRE.BORNES(0;65536);4);"-4";
DECHEX(ALEA.ENTRE.BORNES(0;4096);3);"-";
DECHEX(ALEA.ENTRE.BORNES(0;4294967296);8);
DECHEX(ALEA.ENTRE.BORNES(0;65536);4))

For creating a new UUID, simply exit the cell edit mode, while you view the cell, press F7 key, this will automatically calculate a new UUID.

Attribution to Matt for the inspiration about this formula. Which I adapted for both UUID version 4 format and the maximum amount of Hex values. Which I assume is 16^8 = 4294967296, 16^4 = 65536, 16^3 = 4096.

-1

The formula for Polish version:

=ZŁĄCZ.TEKSTY(
    DZIES.NA.SZESN(LOS.ZAKR(0;4294967295);8);"-";
    DZIES.NA.SZESN(LOS.ZAKR(0;42949);4);"-";
    DZIES.NA.SZESN(LOS.ZAKR(0;42949);4);"-";
    DZIES.NA.SZESN(LOS.ZAKR(0;42949);4);"-";
    DZIES.NA.SZESN(LOS.ZAKR(0;4294967295);8);
    DZIES.NA.SZESN(LOS.ZAKR(0;42949);4)
)
1
-3

ESP:

=CONCATENAR(
    DEC.A.HEX(ALEATORIO.ENTRE(0;4294967295);8);"-"; 
    DEC.A.HEX(ALEATORIO.ENTRE(0;42949);4);"-"; 
    DEC.A.HEX(ALEATORIO.ENTRE(0;42949);4);"-"; 
    DEC.A.HEX(ALEATORIO.ENTRE(0;42949);4);"-"; 
    DEC.A.HEX(ALEATORIO.ENTRE(0;4294967295);8); 
    DEC.A.HEX(ALEATORIO.ENTRE(0;42949);4)
)
1
-3

Italian version:

=CONCATENA(
    DECIMALE.HEX(CASUALE.TRA(0;4294967295);8);"-";
    DECIMALE.HEX(CASUALE.TRA(0;42949);4);"-";
    DECIMALE.HEX(CASUALE.TRA(0;42949);4);"-";
    DECIMALE.HEX(CASUALE.TRA(0;42949);4);"-";
    DECIMALE.HEX(CASUALE.TRA(0;4294967295);8);
    DECIMALE.HEX(CASUALE.TRA(0;42949);4))
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.