+ Reply to Thread
Results 1 to 4 of 4

Filling sheet with unique random numbers

Hybrid View

  1. #1
    Registered User
    Join Date
    06-27-2009
    Location
    USA, PA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Filling sheet with unique random numbers

    I need a macros to fill 2000 rows with unique random numbers from 1 to 19 in B2:T2001 area.
    Every row like this
    14 11 12 7 18 13 19 5 6 16 9 4 8 15 1 3 10 2 17
    Actually it's about random positions in rows for numbers from 1 to 19.
    Different solutions are welcome.
    Thanks.
    Last edited by rnd37; 06-27-2009 at 07:53 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Filling sheet with unique random numbers

    Select B2:T2 and array-enter either =RandSeq() or =RandSeq(True) (the latter if you want the sequence to change each time you calculate.

    Then drag down as far as you want.

    Here's the function:
    Function RandSeq(Optional bVolatile As Boolean = False) As Long()
        ' shg 2006-0307
        ' Worksheet function *only*
    
        ' Returns a random sequence from 1 to N to the calling range
        ' (entered in a single cell, returns 1)
    
        If bVolatile Then Application.Volatile
    
        Dim nRow    As Long     ' count of rows
        Dim nCol    As Long     ' count of columns
    
        Dim afRnd() As Single   ' random rumbers
        Dim nNum    As Long     ' count of afRnd
        Dim iRnd    As Long     ' index to afRnd
        Dim jRnd    As Long     ' secondary index to afRnd
    
        Dim aiRank() As Long    ' rank array
        Dim iRow    As Long     ' row index to aiRank
        Dim iCol    As Long     ' column index to aiRank
    
        ' verify that the caller is a range
        If Not TypeOf Application.Caller Is Range Then Exit Function
    
        With Application.Caller
            nNum = .Count
            nRow = .Rows.Count
            nCol = .Columns.Count
        End With
    
        ' size arrays to size of range
        ReDim afRnd(0 To nNum - 1)
        ReDim aiRank(0 To nRow - 1, 0 To nCol - 1)
    
        ' get random numbers
        For iRnd = 0 To nNum - 1
            afRnd(iRnd) = Rnd
        Next iRnd
    
        ' rank 'em
        For iRnd = 0 To nNum - 1
            iRow = iRnd \ nCol
            iCol = iRnd Mod nCol
            For jRnd = 0 To nNum - 1
                If afRnd(iRnd) >= afRnd(jRnd) Then aiRank(iRow, iCol) = aiRank(iRow, iCol) + 1
            Next
        Next
    
        RandSeq = aiRank
    End Function
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    06-27-2009
    Location
    USA, PA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Filling sheet with unique random numbers

    Just tried this UDF. Works great and very fast. Thanks shg!
    Need to fill 1458 sheets, so will ask for help if it gives any repetitions.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Filling sheet with unique random numbers

    You're welcome.
    ... will ask for help if it gives any repetitions
    It's certainly possible, but the chances are way beyond slight in 2000 rows x 1458 sheets.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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