The function below will enter a random sequence in whatever range it is entered. It must be entered as an array formula.
If you don't want the sequence to change every time the sheet calculates, comment out the Application.Volatile line.
Function RandSeq() As Variant
' Returns a random sequence from 1 to n 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
' only via Ctrl-Alt-F9
Application.Volatile
Dim r As Range
Dim i As Long, j As Long, n As Long
Dim iV As Long, nV As Long
Dim iH As Long, nH As Long
Dim adRand() As Double ' random rumbers
Dim adRank() As Integer ' rank
' verify that the caller is a range
If TypeName(Application.Caller) <> "Range" Then Exit Function '----------->
Set r = Application.Caller
nV = r.Rows.Count
nH = r.Columns.Count
n = nV * nH
' size arrays to size of range
ReDim adRand(0 To nV * nH - 1)
ReDim adRank(0 To nV - 1, 0 To nH - 1)
' get random numbers
Randomize
For i = 0 To n - 1
adRand(i) = Rnd
Next i
' rank 'em
For i = 0 To n - 1
iV = i \ nH
iH = i Mod nH
For j = 0 To n - 1
If adRand(i) >= adRand(j) Then adRank(iV, iH) = adRank(iV, iH) + 1
Next
Next
RandSeq = adRank
End Function
Bookmarks