How to create a GUID in Excel?

后端 未结 14 2202
天涯浪人
天涯浪人 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:47

    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.

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

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

    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

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

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

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

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