+ Reply to Thread
Results 1 to 7 of 7

5-digit Random Unique Alphanumeric Code

  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

    Please Login or Register  to view this content.
    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,258

    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.
    Please Login or Register  to view this content.
    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. [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
  2. Replies: 20
    Last Post: 09-27-2019, 10:11 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