如何在Excel中生成GUID?

我有一个在每行一个订单的Excel文件,我希望每个订单都有一个唯一的标识符,所以会有一个唯一的ID列。 每次填充行时,我都希望Excel为我自动填充唯一标识列。 我做了一些研究,并指出了GUID的方向。 我发现了下面的代码:

Function GenGuid() As String Dim TypeLib As Object Dim Guid As String Set TypeLib = CreateObject("Scriptlet.TypeLib") Guid = TypeLib.Guid ' format is {24DD18D4-C902-497F-A64B-28B2FA741661} Guid = Replace(Guid, "{", "") Guid = Replace(Guid, "}", "") Guid = Replace(Guid, "-", "") GenGuid = Guid End Function 

但我不知道如何实现它。 任何帮助将不胜感激。 先谢谢你。

以下Excelexpression式计算为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))

– 或(取决于区域设置/小数和列表分隔符) –

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

请注意,第三组的第一个字符总是4,表示根据RFC 4122第4.4节的V4(伪随机数生成)GUID / UUID。

还要注意的是,第四组的第一个字符总是在8到B之间。

标准免责声明:由此产生的GUID / UUID不具有密码强度。

我在v.2013 excel vba中使用了以下函数来创build一个GUID,并且工作正常。

 Public Function GetGUID() As String GetGUID = Mid$(CreateObject("Scriptlet.TypeLib").GUID, 2, 36) End Function 

我知道这个问题已经回答了,但是我认为有问题的代码应该看起来像这个页面上的内容: http : //snipplr.com/view/37940/

还没有testing过,但是这段代码似乎进入了Windows API来获取它的GUID – 我会尝试把它放在一个公共模块中,然后在Excel单元格中input=GetGUId()来查看我会得到的结果。 如果它在VB6中有效,那么它在VBA中也有很好的机会:

 Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(7) As Byte End Type Private Declare Function CoCreateGuid Lib "OLE32.DLL" (pGuid As GUID) As Long Public Function GetGUID() As String '(c) 2000 Gus Molina Dim udtGUID As GUID If (CoCreateGuid(udtGUID) = 0) Then GetGUID = _ String(8 - Len(Hex$(udtGUID.Data1)), "0") & Hex$(udtGUID.Data1) & _ String(4 - Len(Hex$(udtGUID.Data2)), "0") & Hex$(udtGUID.Data2) & _ String(4 - Len(Hex$(udtGUID.Data3)), "0") & Hex$(udtGUID.Data3) & _ IIf((udtGUID.Data4(0) < &H10), "0", "") & Hex$(udtGUID.Data4(0)) & _ IIf((udtGUID.Data4(1) < &H10), "0", "") & Hex$(udtGUID.Data4(1)) & _ IIf((udtGUID.Data4(2) < &H10), "0", "") & Hex$(udtGUID.Data4(2)) & _ IIf((udtGUID.Data4(3) < &H10), "0", "") & Hex$(udtGUID.Data4(3)) & _ IIf((udtGUID.Data4(4) < &H10), "0", "") & Hex$(udtGUID.Data4(4)) & _ IIf((udtGUID.Data4(5) < &H10), "0", "") & Hex$(udtGUID.Data4(5)) & _ IIf((udtGUID.Data4(6) < &H10), "0", "") & Hex$(udtGUID.Data4(6)) & _ IIf((udtGUID.Data4(7) < &H10), "0", "") & Hex$(udtGUID.Data4(7)) End If End Function 

感谢Gus Molina!

如果这个代码有效(我不怀疑),我想你会得到一个新的一组GUID的function得到评估,这意味着每次工作表得到计算 – 例如,当您保存工作簿。 确保复制粘贴特定值,如果你需要的GUID的以后使用…这是有点可能。

德语 Excel版本也一样:

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

基于使用Rnd()函数生成随机数的VBA方法,而不是外部API调用或Scriptlet.TypeLib

 Public Function CreateGUID() As String Do While Len(CreateGUID) < 32 If Len(CreateGUID) = 16 Then '17th character holds version information CreateGUID = CreateGUID & Hex$(8 + CInt(Rnd * 3)) End If CreateGUID = CreateGUID & Hex$(CInt(Rnd * 15)) Loop CreateGUID = "{" & Mid(CreateGUID, 1, 8) & "-" & Mid(CreateGUID, 9, 4) & "-" & Mid(CreateGUID, 13, 4) & "-" & Mid(CreateGUID, 17, 4) & "-" & Mid(CreateGUID, 21, 12) & "}" End Function 

这本质上是一个NekojiruSou的答案VBA实现(它也生成一个V4的GUID),并具有相同的限制,但将工作在VBA,可能会更容易实现。

请注意,您可以省略最后一行,以便在结果中不返回破折号和花括号。

如果您将logging插入到数据库中,则可以使用此方法创build一个GUID。

这可能是最简单和最简单的实现方式,因为您不需要复杂的VBA函数就可以使用内置的SQL函数。

该语句使用NewID()

语法如下,

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

VBA语法中,如下所示,

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

如果你需要连接值,只要把它当作一个string来处理,并像通常的SQL语句那样连接起来,

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

我最近在一些vba代码中遇到了使用CreateObject(“Scriptlet.TypeLib”)的问题。

所以基于NekojiruSou excel函数写了以下应该工作,没有任何特定的Excel函数。 这可以用来在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 
 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