Link to home
Create AccountLog in
Avatar of pwanveer
pwanveerFlag for United States of America

asked on

Can Excel or VB create GUIDs?

I have an application I am building in Excel which I believe will be migrated to SQL or Access in the future. I am using Worksheets as Tables and need to assign unique ID's in a number of places. I would like to use GUIDs from inception so as to simplify the migration process as much as possible. Can Excel of VBA auto create GUIDs?
ASKER CERTIFIED SOLUTION
Avatar of Mark_FreeSoftware
Mark_FreeSoftware
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer

or if you don't feel like checking that, use this code:

Declare Function CoCreateGuid_Alt Lib "OLE32.DLL" Alias "CoCreateGuid" (pGuid _
    As Any) As Long
Declare Function StringFromGUID2_Alt Lib "OLE32.DLL" Alias "StringFromGUID2" _
    (pGuid As Any, ByVal address As Long, ByVal Max As Long) As Long

Function CreateGUID() As String
    Dim res As String, resLen As Long, guid(15) As Byte
    res = Space$(128)
    CoCreateGuid_Alt guid(0)
    resLen = StringFromGUID2_Alt(guid(0), ByVal StrPtr(res), 128)
    CreateGUID = Left$(res, resLen - 1)
End Function
Or, more simply:

    Set oTypeLib = CreateObject("Scriptlet.TypeLib")
    szMyGuid = oTypeLib.Guid
    '...
    Set oTypeLib = Nothing
   
HTH

J.
Avatar of pwanveer

ASKER

A quick download of the Class Module and a quick code edit and I'm done! You are a rockstar! Thank you.


thanx for the points and happy coding!