How can I generate GUIDs in Excel?

前端 未结 11 928
别那么骄傲
别那么骄傲 2020-11-27 14:58

I have an excel file with one order on each row, and I want each order to have a unique identifier, so there will be a Unique ID column. Every time I fill a row, I want Exce

相关标签:
11条回答
  • 2020-11-27 15:41

    The following Excel expression evaluates to a V4 GUID:

    =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))

    -or (depending on locale setting/decimal and list separators)-

    =CONCATENATE(DEC2HEX(RANDBETWEEN(0;4294967295);8);"-";DEC2HEX(RANDBETWEEN(0;65535);4);"-";DEC2HEX(RANDBETWEEN(16384;20479);4);"-";DEC2HEX(RANDBETWEEN(32768;49151);4);"-";DEC2HEX(RANDBETWEEN(0;65535);4);DEC2HEX(RANDBETWEEN(0;4294967295);8))

    Note that the first character of the third group is always 4 to signify a V4 (pseudo-random number generated) GUID/UUID per RFC 4122 section 4.4.

    Also note that the first character of the fourth group is always between 8 and B per the same RFC.

    Standard disclaimer: the resulting GUIDs/UUIDs are not cryptographically strong.

    0 讨论(0)
  • 2020-11-27 15:42

    I recently ran into problems using CreateObject("Scriptlet.TypeLib") in some vba code.

    So based on NekojiruSou excel functions wrote the following which should work without any specific excel functions. This can be used to develop a user defined function in excel.

    Public Function Get_NewGUID() As String
        'Returns GUID as string 36 characters long
    
        Randomize
    
        Dim r1a As Long
        Dim r1b As Long
        Dim r2 As Long
        Dim r3 As Long
        Dim r4 As Long
        Dim r5a As Long
        Dim r5b As Long
        Dim r5c As Long
    
        'randomValue = CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) + lowerbound
        r1a = RandomBetween(0, 65535)
        r1b = RandomBetween(0, 65535)
        r2 = RandomBetween(0, 65535)
        r3 = RandomBetween(16384, 20479)
        r4 = RandomBetween(32768, 49151)
        r5a = RandomBetween(0, 65535)
        r5b = RandomBetween(0, 65535)
        r5c = RandomBetween(0, 65535)
    
        Get_NewGUID = (PadHex(r1a, 4) & PadHex(r1b, 4) & "-" & PadHex(r2, 4) & "-" & PadHex(r3, 4) & "-" & PadHex(r4, 4) & "-" & PadHex(r5a, 4) & PadHex(r5b, 4) & PadHex(r5c, 4))
    
    End Function
    
    Public Function Floor(ByVal X As Double, Optional ByVal Factor As Double = 1) As Double
        'From: http://www.tek-tips.com/faqs.cfm?fid=5031
        ' X is the value you want to round
        ' Factor is the multiple to which you want to round
            Floor = Int(X / Factor) * Factor
    End Function
    
    Public Function RandomBetween(ByVal StartRange As Long, ByVal EndRange As Long) As Long
        'Based on https://msdn.microsoft.com/en-us/library/f7s023d2(v=vs.90).aspx
        '         randomValue = CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) + lowerbound
            RandomBetween = CLng(Floor((EndRange - StartRange + 1) * Rnd())) + StartRange
    End Function
    
    Public Function PadLeft(text As Variant, totalLength As Integer, padCharacter As String) As String
        'Based on https://stackoverflow.com/questions/12060347/any-method-equivalent-to-padleft-padright
        ' with a little more checking of inputs
    
        Dim s As String
        Dim inputLength As Integer
        s = CStr(text)
        inputLength = Len(s)
    
        If padCharacter = "" Then
            padCharacter = " "
        ElseIf Len(padCharacter) > 1 Then
            padCharacter = Left(padCharacter, 1)
        End If
    
        If inputLength < totalLength Then
            PadLeft = String(totalLength - inputLength, padCharacter) & s
        Else
            PadLeft = s
        End If
    
    End Function
    
    Public Function PadHex(number As Long, length As Integer) As String
        PadHex = PadLeft(Hex(number), 4, "0")
    End Function
    
    0 讨论(0)
  • 2020-11-27 15:43

    Since windows update taken out "Scriptlet.TypeLib", try the following:

    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
    

    Alternatively,

    if you are connecting to SQL Server 2008 or higher, try to use the SQL NEWID() function instead.

    0 讨论(0)
  • 2020-11-27 15:51

    Same same for german Excel version:

    =VERKETTEN(DEZINHEX(ZUFALLSBEREICH(0;4294967295);8);"-";DEZINHEX(ZUFALLSBEREICH(0;65535);4);"-";DEZINHEX(ZUFALLSBEREICH(16384;20479);4);"-";DEZINHEX(ZUFALLSBEREICH(32768;49151);4);"-";DEZINHEX(ZUFALLSBEREICH(0;65535);4);DEZINHEX(ZUFALLSBEREICH(0;4294967295);8))
    
    0 讨论(0)
  • 2020-11-27 15:51

    If you are inserting records into a database you can use this way to make a GUID.

    It is probably the most simplest and easiest way to implement as you don't need a complex VBA function as you use the built in SQL function.

    The statement uses NewID(),

    The syntax is as follows,

    INSERT INTO table_name (ID,Column1,Column2,Column3)
    VALUES (NewID(),value1,value2,value3) 
    

    In VBA syntax it is as follows,

    strSql = "INSERT INTO table_name " _
           & "(ID,Column1,Column2,Column3) " _
           & "VALUES (NewID(),value1,value2,value3)"
    

    And if you need to concatenate values, just treat it as a string and concatenate as you would normally for a SQL statement,

    strSql = "INSERT INTO table_name " _
           & "(ID,Column1,Column2,Column3) " _
           & "VALUES (" & "NewID()" & "," & "value1" & "," & "value2" & "," & "value3" & ")"
    
    0 讨论(0)
  • 2020-11-27 15:51
    Function funGetGuid() As String
    
        Const URL As String = "http://www.guidgen.com/"
        Const strMask As String = "value="
    
        Dim l As Long
        Dim txt As String
    
        With CreateObject("MSXML2.XMLHTTP")
            .Open "GET", URL, False
            .send
            txt = .responseText
        End With
    
        Do
            l = InStr(l + 1, txt, strMask)
            If l = 0 Then Exit Do
            funGetGuid = Mid$(txt, l + Len(strMask) + 1, 36)
        Loop
    
    End Function
    
    0 讨论(0)
提交回复
热议问题