You can enter the function below as an array formula in the range where you wish non-repeated numbers to appear. For example, in A1:A10, enter =RandSeq()
Array formulas MUST be confirmed with Ctrl+Shift+Enter, not Enter. You'll know you did it correctly if curly braces appear around the formula in the Formula Bar; you cannot type in the braces directly.
Function RandSeq() As Variant
' shg 2006-0307
' Worksheet function *only*
' Returns a random sequence from 1 to nRnd to the calling range
' (entered in a single cell, returns 1)
' The line below will cause the sequence to recalculate whenever anything
' calculates. Comment it out if you only want the sequence to change
' via Ctrl-Alt-F9
Application.Volatile
Dim nRow As Long ' count of rows
Dim nCol As Long ' count of columns
Dim afRnd() As Single ' random rumbers
Dim nRnd 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 TypeName(Application.Caller) <> "Range" Then Exit Function '--------->
With Application.Caller
If .Areas.Count > 1 Then Exit Function '------------------------------->
nRow = .Rows.Count
nCol = .Columns.Count
End With
nRnd = nRow * nCol
' size arrays to size of range
ReDim afRnd(0 To nRnd - 1)
ReDim aiRank(0 To nRow - 1, 0 To nCol - 1)
' get random numbers
Randomize
For iRnd = 0 To nRnd - 1
afRnd(iRnd) = Rnd
Next iRnd
' rank 'em
For iRnd = 0 To nRnd - 1
iRow = iRnd \ nCol
iCol = iRnd Mod nCol
For jRnd = 0 To nRnd - 1
If afRnd(iRnd) >= afRnd(jRnd) Then aiRank(iRow, iCol) = aiRank(iRow, iCol) + 1
Next
Next
RandSeq = aiRank
End Function
Bookmarks