+ Reply to Thread
Results 1 to 10 of 10

Create a GUID in entire column

Hybrid View

tvainisi Create a GUID in entire column 10-13-2009, 10:41 AM
tvainisi Re: Create a GUID in entire... 10-13-2009, 11:22 AM
Leith Ross Re: Create a GUID in entire... 10-13-2009, 12:15 PM
tvainisi Re: Create a GUID in entire... 10-13-2009, 02:17 PM
Leith Ross Re: Create a GUID in entire... 10-13-2009, 02:52 PM
Dirtnap Re: Create a GUID in entire... 10-21-2010, 09:27 AM
ranbaker100 Re: Create a GUID in entire... 02-07-2013, 06:04 PM
  1. #1
    Registered User
    Join Date
    10-13-2009
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2003
    Posts
    4

    Create a GUID in entire column

    Hi Folks,

    I've done a lot of programming and scripting, but none in Excel. I have a little experience with the VBA interface as well.

    I have a spreadsheet that I use to populate a MySql database. It has a hierarchical relationship in the spreadsheet which is accomplished through a column called GUID and another column called Parent GUID. I've been using a website to generate GUIDs for me and pasting them into the first column one at a time, then copying to the second column where necessary. It takes forever.

    So, I'm wondering if I can write (or better - paste!) a function that will automatically generate the first column of GUIDs. I've googled and found several functions that create a guid, but I don't really understand how to call the function from excel. Also, I don't want to manually call it thousands of times. I'd just like to call it once and have it generate GUIDs for the entire column.

    I'll still copy and paste to fill in the Parent GUID column.

    Thanks in advance for any help.

  2. #2
    Registered User
    Join Date
    10-13-2009
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Create a GUID in entire column

    okay... well i hate waiting so I started to solve this myself

    here's what i put in the vba code window:
    Declare Function CoCreateGuid Lib "ole32" (ByRef GUID As Byte) As Long
    
    Function MakeGUID()
      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))
      Next N
      MakeGUID = GUID
    End Function
    
    Sub makeAllGUIDS()
      Sheets("Sheet1").Select
      Range("A1").Select
      
      Do Until Selection.Offset(0, 1).Value = ""
        Selection.Value = MakeGUID()
        Selection.Offset(1, 0).Select
      Loop
      
      Range("B1").Select
        
    End Sub
    Im not sure I trust that GUID function to be unique though, which is a huge problem.
    Last edited by Leith Ross; 10-13-2009 at 12:12 PM. Reason: Added Code Tags

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Create a GUID in entire column

    Hello tvainisi,

    Here is how to correctly generate the GUID (Globally Unique Identifier) or CLSID (Class ID). Place this code in its own module in your VBA project.
    'Written: October 12, 2009
    'Author:  Leith Ross
    'Summary: Function creates a GUID (aka CLSID) and returns it as a string.
    
    Private Type GUID
       Data1 As Long
       Data2 As Long
       Data3 As Long
       Data4(8) As Byte
    End Type
    
    Private Declare Function CoCreateGuid _
      Lib "ole32.dll" _
      (ByRef pGUID As GUID) As Long
    
    Private Declare Function StringFromGUID2 _
      Lib "ole32.dll" _
        (ByRef rGUID As Any, _
         ByVal lpstrCLSID As Long, _
         ByVal cbMax As Long) As Long
    
    Function CreateGUID() As String
    
      Dim b() As Byte
      Dim BuffSize As Long
      Dim RetVal As Long
      Dim MyGUID As GUID
    
        BuffSize = 40
        ReDim b(BuffSize * 2) As Byte
        
        RetVal = CoCreateGuid(MyGUID)
        RetVal = StringFromGUID2(MyGUID, VarPtr(b(0)), BuffSize)
        CreateGUID = Left$(b, RetVal - 1)
    
    End Function
    Because this is a UDF (User Defined Function) you can place it in cell to return a new GUID to the cell.
       A1 = CreateGUID()
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    10-13-2009
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Create a GUID in entire column

    Thanks for the assistance Leith.

    Is there a way to not have the curly brackets around the GUID, or do I just need to use some string functions to remove them?
    Last edited by tvainisi; 10-13-2009 at 02:19 PM.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Create a GUID in entire column

    Hello tvainisi,

    The GUID includes the curly braces. So use a Mid function to remove them. Change the last line in the macro to this...
    CreateGUID = Mid(b, 2, RetVal - 3)
    Last edited by Leith Ross; 10-13-2009 at 02:53 PM. Reason: Corrected spelling

  6. #6
    Registered User
    Join Date
    10-13-2009
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Create a GUID in entire column

    Terrific! Thanks a bunch!

  7. #7
    Registered User
    Join Date
    10-20-2010
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Create a GUID in entire column

    My apologies - the 2009 reference didn't even register. I will read the rules.

    And thanks for the suggested function.
    Last edited by Dirtnap; 10-21-2010 at 09:32 AM.

  8. #8
    Registered User
    Join Date
    09-21-2012
    Location
    Tallahassee, FL
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Create a GUID in entire column

    I am getting an error on Windows 7 on (Compile Error: Type Mismatch on VarPtr) - I am using Option Explicit. the Definition says it is hidden:

    RetVal = StringFromGUID2(MyGUID, VarPtr(b(0)), BuffSize)
    thanks for the help

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1