+ Reply to Thread
Results 1 to 7 of 7

5-digit Random Unique Alphanumeric Code

Hybrid View

  1. #1
    Registered User
    Join Date
    07-13-2015
    Location
    Chicago, IL
    MS-Off Ver
    2013
    Posts
    14

    5-digit Random Unique Alphanumeric Code

    I'm looking to create a 5-digit random but unique alphanumeric code in excel. I'm trying to keep the format of letter, number, letter, number, letter. Examples: d5s3e, h2x0f, j5n9n, d0a6z, y8c1p, q6z3b. I'm trying to stay away from the RANDBETWEEN function but am not sure how to craft a macro or something in the VBA that might get me what I want. The biggest problems is creating the random codes but not creating any duplicates. Any help would be awesome!

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: 5-digit Random Unique Alphanumeric Code

    Here you go. You didn't specify where you wanted the output, nor how many codes you want, so this code generates 1 code and puts it in a messagebox.

    As for not creating duplicates, this code doesn't address this for the same omissions mentioned above. However, in generating just 2 codes, there is a ~1 in 1.75 million chance that the codes would be the same, so really I'm not sure that duplicate checking is really necessary unless you're generating a massive amount of codes.

    However, if you do specify how you want the output, checking for duplicates would be fairly easy with .Find

    Sub mcodden()
    code = ""
    For i = 1 To 5
        If i Mod 2 = 0 Then
            code = code & Int((9 - 0 + 1) * Rnd)
        Else
            code = code & Chr(Int((122 - 97 + 1) * Rnd + 97))
        End If
    Next
    MsgBox code
    End Sub
    Last edited by walruseggman; 07-15-2015 at 01:58 PM.

  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: 5-digit Random Unique Alphanumeric Code

    Hello walruseggman,

    An easy method of avoiding duplicates is to add each code to a Collection. The collection object will generate a trappable error if the code already exists. The collection items can then be retrieved later.
    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
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: 5-digit Random Unique Alphanumeric Code

    @ Leith Ross: good idea, a dictionary would work well, I suppose to you could also use an array and then Ubound(Filter()) to check the array for existing code.

    I'm guessing a dictionary would be the fastest method though between dictionary, array, and .Find

  5. #5
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: 5-digit Random Unique Alphanumeric Code

    DUPLICATE:

    http://www.excelforum.com/excel-prog...eric-code.html

    OP - please don't duplicate questions.
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  6. #6
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: 5-digit Random Unique Alphanumeric Code

    Hi mcodden,

    The following Macro should help you get started.
    Sub GenerateUniqueRandomValue()
    
      Dim ws As Worksheet
    
      Dim r As Range
      Dim iLastRow As Long
      Dim x1 As Long
      Dim x2 As Long
      Dim x3 As Long
      Dim x4 As Long
      Dim x5 As Long
      Dim bNeedMore As Boolean
      Dim sValue As String
      
      'ASCII Table Reference: http://www.asciitable.com/
      
    
      'Create the Worksheet object
      Set ws = ThisWorkbook.Sheets("Sheet1")
    
      
      'Use the System Timer to Generate the first Random Number
      Call Randomize
      
      'Loop until there is a unique Value
      bNeedMore = True
      While bNeedMore = True
      
      
        'Generate a random number between 0 and 25
        'Create the numerical equivalent of an ASCII character between 'a' and 'z'
        x1 = Int(26 * Rnd()) + Asc("a")
    
        'Generate a random number between 0 and 9
        x2 = Int(10 * Rnd())
    
        'Generate a random number between 0 and 25
        'Create the numerical equivalent of an ASCII character between 'a' and 'z'
        x3 = Int(26 * Rnd()) + Asc("a")
    
        'Generate a random number between 0 and 9
        x4 = Int(10 * Rnd())
    
        'Generate a random number between 0 and 25
        'Create the numerical equivalent of an ASCII character between 'a' and 'z'
        x5 = Int(26 * Rnd()) + Asc("a")
    
        'Create the entire string
        sValue = Chr(x1) & x2 & Chr(x3) & x4 & Chr(x5)
        Debug.Print sValue
      
      
        'Make sure the value is unique in Column 'A'
        'Find the first occurence of the string in Column 'A'
        Set r = Nothing
        Set r = ws.Columns("A").Find(What:=sValue, _
                            After:=ws.Range("A1"), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False, _
                            SearchFormat:=False)
                          
                          
        'Exit if the value is unique
        'Otherwise try again
        If r Is Nothing Then
          bNeedMore = False
        End If
     
      Wend
     
     'Find the last item in Column 'A' (must be row 1 or more)
     'Runtime error is generated if the column is Empty
     On Error Resume Next
     iLastRow = ws.Range("A:A").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
     If iLastRow < 1 Then
       iLastRow = 1
     End If
     On Error GoTo 0
     
     'Put the value in Column 'A' in the next row
     ws.Cells(iLastRow + 1, "A").Value = sValue
     
     
     'Clear object pointers
     Set r = Nothing
     Set ws = Nothing
      
    End Sub
    Lewis

    This is a duplicate thread. See http://www.excelforum.com/excel-form...ml#post4132254
    Last edited by LJMetzger; 07-15-2015 at 02:26 PM.

  7. #7
    Registered User
    Join Date
    07-13-2015
    Location
    Chicago, IL
    MS-Off Ver
    2013
    Posts
    14

    Re: 5-digit Random Unique Alphanumeric Code

    Lewis,

    That Macro worked perfectly! Thank you so much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 22
    Last Post: 06-27-2024, 10:35 AM
  2. [SOLVED] Creating a 4-digit alphanumeric code string in Excel (two variations)
    By mcodden in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-05-2021, 02:52 AM
  3. [SOLVED] 3-5-3 Random, Unique Alphanumeric Code Generation
    By mcodden in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-15-2015, 06:00 PM
  4. [SOLVED] 5-digit Random Unique Alphanumeric Code
    By mcodden in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 07-15-2015, 04:32 PM
  5. Replies: 2
    Last Post: 11-06-2014, 05:07 PM
  6. How to generate unique random alphanumeric 32 character codes?
    By JussiR in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-21-2014, 06:30 AM
  7. Replies: 1
    Last Post: 01-22-2013, 07:02 AM

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