+ Reply to Thread
Results 1 to 6 of 6

Generating of unique random set length numerical codes

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-02-2007
    Location
    Australia NSW
    MS-Off Ver
    2013
    Posts
    494

    Generating of unique random set length numerical codes

    I am after a way to have a cell highlighted to input the code length, between 4 - 7 digits say cell A1
    Each code can only use a numerical value between 0 - 9, and not use the same digit more than once.
    Eg. 6 digit code could be 371409, but could not be something like 374380, as 3 is used twice.

    I would like a random list generated down column A from A2:A1000.

    How would this be accomplished?

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Generating of unique random set length numerical codes

    I know that there is probably an array formula to do this.

    But my prefferred route would be a custom Macro.

    It will take ten minutes to write. BRB


    
    Function RandCode()
    
    Codestring = "0123456789"
    Output = ""
    
    10  If Len(Codestring) = 0 Then GoTo 200
    If Len(Codestring) = 1 Then Output = Output & Codestring: GoTo 200
    pos = Application.RandBetween(1, Len(Codestring))
    Output = Output & Mid(Codestring, pos, 1)
    Codestring1 = Left(Codestring, pos - 1)
    Codestring2 = Right(Codestring, Len(Codestring) - pos)
    Codestring = Codestring1 & Codestring2
    GoTo 10
    
    200 RandCode = "'" & Output
    
    End Function
    Attached Files Attached Files
    Last edited by mehmetcik; 10-02-2014 at 06:47 AM.

  3. #3
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Generating of unique random set length numerical codes

    vba macro
    Sub ddmdm()
    Dim b() As Boolean, j As Long, x As Long, y
    If (Len(Range("A1") > 0)) * (Len(Range("A1") < 10)) Then
    For j = 2 To 1000
        y = vbNullString
        ReDim b(9)
        Do
            x = Int(Rnd * 10)
            If Not b(x) Then y = y & x: b(x) = True
        Loop Until Len(y) = Range("A1").Value
        Range("A" & j) = "'" & y
    Next j
    End If
    End Sub

  4. #4
    Forum Contributor
    Join Date
    01-02-2007
    Location
    Australia NSW
    MS-Off Ver
    2013
    Posts
    494

    Re: Generating of unique random set length numerical codes

    Quote Originally Posted by mehmetcik View Post
    I know that there is probably an array formula to do this.

    But my prefferred route would be a custom Macro.

    It will take ten minutes to write. BRB


    
    Function RandCode()
    
    Codestring = "0123456789"
    Output = ""
    
    10  If Len(Codestring) = 0 Then GoTo 200
    If Len(Codestring) = 1 Then Output = Output & Codestring: GoTo 200
    pos = Application.RandBetween(1, Len(Codestring))
    Output = Output & Mid(Codestring, pos, 1)
    Codestring1 = Left(Codestring, pos - 1)
    Codestring2 = Right(Codestring, Len(Codestring) - pos)
    Codestring = Codestring1 & Codestring2
    GoTo 10
    
    200 RandCode = "'" & Output
    
    End Function
    Thank you very much for the reply, and workbook.
    Seems to be te trick.
    Modified to the below to suit a set code length:
    Function RandCode()
    
    Codestring = "0123456789"
    Output = ""
    
    10  If Len(Codestring) = 0 Then GoTo 200
    If Len(Codestring) = 1 Then Output = Output & Codestring: GoTo 200
    pos = Application.RandBetween(1, Len(Codestring))
    Output = Output & Mid(Codestring, pos, 1)
    Codestring1 = Left(Codestring, pos - 1)
    Codestring2 = Right(Codestring, Len(Codestring) - pos)
    Codestring = Codestring1 & Codestring2
    GoTo 10
    
    200 RandCode = Left("'" & Output, Sheet1.Range("B2"))
    
    End Function
    ONLY issue i am having, is that any changes to other cells in the sheet, changes the codes each time.
    Last edited by coreytroy; 10-03-2014 at 04:20 AM.

  5. #5
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Generating of unique random set length numerical codes

    If the cells are formatted as text then line 200 can be modified to:

    200 RandCode = Output

  6. #6
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Generating of unique random set length numerical codes

    Try this version.

    Two Changes
    1. it stops the UDF working if B2 is changed to 0
    2. it stops picking numbers if it has the number of digits specified by B2
    [ Similar to your modification, but why pick 5 digits if you are going to ditch them?]

    Function RandCode()
    temp = Range(Application.Caller.Address).Text
    If Range("B2") = 0 Then RandCode = Range(Application.Caller.Address).Text: GoTo 300
    Codestring = "0123456789"
    Output = ""
    
    10  If Len(Codestring) = 10 - Sheet1.Range("B2") Then GoTo 200
    If Len(Codestring) = 1 Then Output = Output & Codestring: GoTo 200
    pos = Application.RandBetween(1, Len(Codestring))
    Output = Output & Mid(Codestring, pos, 1)
    Codestring1 = Left(Codestring, pos - 1)
    Codestring2 = Right(Codestring, Len(Codestring) - pos)
    Codestring = Codestring1 & Codestring2
    GoTo 10
    
    200 RandCode = "'" & Output
    300 'Quit
    
    End Function

+ 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. 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
  2. Generating a list of Random numbers of random length...
    By Growl in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-19-2013, 12:10 PM
  3. Replies: 1
    Last Post: 01-22-2013, 03:45 PM
  4. [SOLVED] Generating unique random number
    By Fuhgawz in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-09-2012, 05:26 PM
  5. Generate unique random alphabets for 35 characters in length
    By promo786 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-13-2011, 11:10 PM

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