How to create a GUID in Excel?
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:
=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))
I have been unable to get the concatenate method to work, and so tried it using the "&" figure to concatenate anyway. That seemed to work, but then I get an inexplicable error on the second DEX2HEX
block: DEC2HEX(RANDBETWEEN(0,65535),4)
. The Excel formula evaluator says it's invalid, but I can't seem to figure out why at all. Any ideas?
As of modern version of Excel, there's the syntax with commas, not semicolons. I'm posting this answer for convenience of others so they don't have to replace the strings- We're all lazy... hrmp... human, right?
=CONCATENATE(DEC2HEX(RANDBETWEEN(0,4294967295),8),"-",DEC2HEX(RANDBETWEEN(0,65535),4),"-",DEC2HEX(RANDBETWEEN(0,65535),4),"-",DEC2HEX(RANDBETWEEN(0,65535),4),"-",DEC2HEX(RANDBETWEEN(0,4294967295),8),DEC2HEX(RANDBETWEEN(0,65535),4))
Or, if you like me dislike when a guid screams and shouts and you, we can go lower-cased like this.
=LOWER(CONCATENATE(DEC2HEX(RANDBETWEEN(0,4294967295),8),"-",DEC2HEX(RANDBETWEEN(0,65535),4),"-",DEC2HEX(RANDBETWEEN(0,65535),4),"-",DEC2HEX(RANDBETWEEN(0,65535),4),"-",DEC2HEX(RANDBETWEEN(0,4294967295),8),DEC2HEX(RANDBETWEEN(0,65535),4)))
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
=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)
)
=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.
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,65535)
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)