How to create a GUID in Excel?

后端 未结 14 2201
天涯浪人
天涯浪人 2021-01-30 20:16

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:

=CO         


        
相关标签:
14条回答
  • 2021-01-30 20:40

    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 讨论(0)
  • 2021-01-30 20:43
    =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)
    )
    
    0 讨论(0)
  • 2021-01-30 20:44
    =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.

    0 讨论(0)
  • 2021-01-30 20:44

    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)
    )
    
    0 讨论(0)
  • 2021-01-30 20:44

    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 讨论(0)
  • 2021-01-30 20:45

    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)
    
    0 讨论(0)
提交回复
热议问题