Consulting

Results 1 to 5 of 5

Thread: Generate Unique Reference Numbers on a userform

  1. #1
    VBAX Regular
    Joined
    Sep 2011
    Posts
    17
    Location

    Generate Unique Reference Numbers on a userform

    Hello,
    I am wondering if anyone can suggest a way to generate a unique reference when saving the data on a userform to a workbook.

    Cheers

    Dave

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Look up Generate GUID on Google.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Sep 2011
    Posts
    17
    Location
    Hi Thanks for that but it seems like overkill for what I need. I just want to assign a unique ref number to the row when writing it to the workbook.
    EG 001, 002, 003

    Cheers

    Dave

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Put a value of say 1 in Sheet1!A2 and set the cell number custom format to: "EG" 000

    Then run the sub. The values are actually numbers.

    [vba]Sub SetNextNumber()
    Dim theRange As Range, theCell As Range, nextLong As Long
    Set theRange = Sheet1.Range("A2", Sheet1.Range("A" & Rows.Count).End(xlUp))
    Set theCell = Sheet1.Range("A" & Rows.Count).End(xlUp).Offset(1)
    nextLong = NextNumber(theRange)
    theCell.Value = nextLong
    theCell.NumberFormat = """EG"" 000"
    End Sub

    Function NextNumber(aRange As Range) As Long
    NextNumber = WorksheetFunction.Max(aRange) + 1
    End Function
    [/vba]

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    NOW will generate a unique number

Posting Permissions

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