Filehost.ro - gazduire fisiere
Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Nou pe simpatie:
Anna281 pe Simpatie
Femeie
24 ani
Iasi
cauta Barbat
26 - 45 ani
Mrrrr's Forum (VIEW ONLY)ReguliInregistrareLoginPozeNu sunteti logat. Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides /

[EXCEL] Generate Unique Random GUID Codes

Pagini: 1 Moderat de TRaP, TonyTzu
#1
TRaP
Moderator
Postari: 960
Source:
https://www.excelforum.com/excel-progra ... f3916.html

There is also VBA in the source above, but the simplest would be with the formula below, the only problem being that it changes every time the sheet is recalculated:
=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))

VBA solution below - paste into a regular module

Option Explicit
Public Function CreateGUID(Optional IncludeHyphens As Boolean = True, _
                           Optional IncludeBraces As Boolean = False) _
                           As String
   
    Dim obj As Object
    Dim strGUID As String
   
    'Late-bind obj as a TypeLib -- a rare time when late-binding
    'is actually a must!
    '
    'No matter how much experimenting I did, the Scriptlet object
    'could simply not be pummeled into creating a GUID without
    'a run-time error... See more discussion here:
    '
    'https://groups.google.com/forum/#!topic/microsoft.public.scripting.wsh/DWUq-tRLsOo
    '
    'All the way back to '99!
    Set obj = CreateObject("Scriptlet.TypeLib")
   
    'Assign the raw GUID, minus the last two erroneous chars
    strGUID = Left(obj.GUID, Len(obj.GUID) - 2)
   
    'If IncludeHyphens is switched from the default True to False,
    '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
   
    CreateGUID = strGUID
   
End Function
Public Sub TestCreateGUID()

    'Dim t As Scriptlet.TypeLib <~ error, user type not defined
    'Dim t As Scriptlet.IGenScriptletTLib <~ at least compiles
    'Debug.Print t.Name <~ error, object or with block variable not set
    '                      i.e. doesn't understand t as a variable
    'Debug.Print t.GUID <~ same
    'Debug.Print t.AnyOtherListedMethodOnThisObject
   
    Dim GUID As String
   
    GUID = CreateGUID() '<~ default
    Sheets("Sheet1").Range("A1").Value = GUID
   
    '^^ the result of the above looks like this:
    '5DC4C0BD-2AD2-4F36-A37D-1851F5B09966
   
    GUID = CreateGUID(IncludeHyphens:=False) '<~ no hyphens here
    Sheets("Sheet1").Range("A2").Value = GUID
   
    '^^ the result of the above looks like this:
    '6A5A9CF9E76948859A0666BA1496D7F0
    '
    'i.e. hyphens removed
   
    GUID = CreateGUID(IncludeBraces:=True) '<~ curly braces on the ends
    Sheets("Sheet1").Range("A3").Value = GUID
   
    '^^ the result of the above looks like this:
    '{88B6591C-053D-414E-8447-224704047786}
    '
    'i.e. curly braces remain
   
    GUID = CreateGUID(IncludeHyphens:=False, IncludeBraces:=True)
    Sheets("Sheet1").Range("A4").Value = GUID
   
    '^^ the result of the above looks like this:
    '{88B6591C053D414E8447224704047786}
    '
    'i.e. curly braces remain but hyphens do not

End Sub


 
   
Pagini: 1  
Mergi la