Public Function getRandList(iLow As Long, iHigh As Long) As Variant
Application.Volatile
Dim v As Variant
Dim iDim As Long
Dim i As Long
iDim = iHigh - iLow + 1
ReDim v(1 To iDim, 1 To 2)
For i = LBound(v, 1) To UBound(v, 1)
v(i, 1) = iLow + i - LBound(v, 1)
v(i, 2) = Rnd
Next i
Call procSort2D(v, "A", 2, LBound(v, 1), UBound(v, 1))
getRandList = v
End Function
Sub procSort2D(ByRef avArray, sOrder As String, iKey As Integer, iLow1 As Integer, iHigh1 As Integer)
On Error Resume Next
'Dimension variables
Dim iLow2 As Integer, iHigh2 As Integer, i As Integer
Dim vItem1, vItem2 As Variant
'Set new extremes to old extremes
iLow2 = iLow1
iHigh2 = iHigh1
'Get value of array item in middle of new extremes
vItem1 = avArray((iLow1 + iHigh1) \ 2, iKey)
'Loop for all the items in the array between the extremes
While iLow2 < iHigh2
If sOrder = "A" Then
'Find the first item that is greater than the mid-point item
While avArray(iLow2, iKey) < vItem1 And iLow2 < iHigh1
iLow2 = iLow2 + 1
Wend
'Find the last item that is less than the mid-point item
While avArray(iHigh2, iKey) > vItem1 And iHigh2 > iLow1
iHigh2 = iHigh2 - 1
Wend
Else
'Find the first item that is less than the mid-point item
While avArray(iLow2, iKey) > vItem1 And iLow2 < iHigh1
iLow2 = iLow2 + 1
Wend
'Find the last item that is greater than the mid-point item
While avArray(iHigh2, iKey) < vItem1 And iHigh2 > iLow1
iHigh2 = iHigh2 - 1
Wend
End If
'If the two items are in the wrong order, swap the rows
If iLow2 < iHigh2 Then
For i = 1 To UBound(avArray, 2)
vItem2 = avArray(iLow2, i)
avArray(iLow2, i) = avArray(iHigh2, i)
avArray(iHigh2, i) = vItem2
Next
End If
'If the pointers are not together, advance to the next item
If iLow2 <= iHigh2 Then
iLow2 = iLow2 + 1
iHigh2 = iHigh2 - 1
End If
Wend
'Recurse to sort the lower half of the extremes
If iHigh2 > iLow1 Then procSort2D avArray, sOrder, iKey, iLow1, iHigh2
'Recurse to sort the upper half of the extremes
If iLow2 < iHigh1 Then procSort2D avArray, sOrder, iKey, iLow2, iHigh1
End Sub