Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error 9: Subject out of range using QuickSort routine 1

Status
Not open for further replies.

cactus13

Automotive
Jul 16, 2001
25
Hello,

I'm trying to sort a list of data and found the following code (for a Quick Sort routine) on the devx domain ( but unfortunatley it's not working in my VBA application. Can some one please help me?

At the bottom is a simple routine called Test(), I wrote just to see if it worked. The function CompMe is not included here but is included in my source.
I first want to try if it works with numbers and than see if it will also work with string which I want to use it with.

When I run the test program and trace back the error I get is a error 9:Subject is out of range. Is this due to it being source code fo VB only,not ment to be implemented in VBA?

I sincerly hop some one can solve the puzzle or ive me a clue, as I don't want to sort manually....

Jonathan

NB This is the source code below, the error line is bold and captured between commentlines.

Public Sub Quicksort(varArray As Variant, ByVal pIndex As Integer, ByVal pFirst As Long, ByVal pLast As Long)

Dim lngPivot As Long

' If the range is valid then sort
If pFirst < pLast Then
' Split the array and return the index of the item
' that is in the correct location
PartitionArray pFirst, pLast, lngPivot, varArray, pIndex
' Sort the lower portion
Quicksort varArray, pIndex, pFirst, lngPivot - 1
' Sort the upper portion
Quicksort varArray, pIndex, lngPivot + 1, pLast
End If

End Sub

Private Sub PartitionArray(ByVal pFirst As Long, ByVal pLast As Long, ByRef
pPivot As Long, varArray As Variant, ByVal pIndex As Integer)

Dim varPivot As Variant
Dim varTemp As Variant
Dim varArrTemp As Variant
Dim i As Long
Dim j As Long
Dim z As Long

' Choose the pivot as the first element in the range

'** In the following line occurs error 9 **

varPivot = varArray(pIndex, pFirst)

'** In the above line occurs error 9 **

i = pFirst
j = pLast + 1

Do
' Loop from the beginning of the range until you
' find a larger element or there are none
Do
i = i + 1
Loop Until CompMe(varArray(pIndex, i), varPivot, False, True) Or i >= pLast

' Loop from the end of the array until you
' find a smaller element or there are none
Do
j = j - 1
Loop Until CompMe(varArray(pIndex, j), varPivot, True, True) Or j <= pFirst

' If they haven't crossed then swap them
If i < j Then
ReDim varArrTemp(UBound(varArray, 1), 0)
For z = LBound(varArray, 1) To UBound(varArray, 1)
varArrTemp(z, 0) = varArray(z, i)
Next z
For z = LBound(varArray, 1) To UBound(varArray, 1)
varArray(z, i) = varArray(z, j)
Next z
For z = LBound(varArray, 1) To UBound(varArray, 1)
varArray(z, j) = varArrTemp(z, 0)
Next z
End If

Loop Until j <= i

' Swap the pivot with the split in the array
varArray(pIndex, pFirst) = varArray(pIndex, j)
varArray(pIndex, j) = varPivot

' Return the index of the element that is in the correct
' location to enable another sort of the two halves
pPivot = j

End Sub

Sub Test()
Dim Lijst(9)
For i = 0 To 9
Lijst(i) = 10 * Rnd
Next i
Quicksort Lijst, 5, 0, 9
End Sub
 
Replies continue below

Recommended for you

Cactus13,

Runtime error 9, &quot;Subscript out of range&quot;, means your code is attempting to reference a non-existent array element. You did not post your array declaration but I would look at how it is DIMensioned. By default, array subscripts begin at zero, unless explicitly dimensioned otherwise or the statement &quot;Option Base 1&quot; is placed at the top of your code module. Given that you probably copied & pasted the Quicksort code, this sounds like an &quot;off by one&quot; error; i.e. the routine references a first and last index of say 1..100 and the array subscript range is actually 0..99. Look at the values of pFirst & pLast you call Quicksort with compared to the array's subscript range.

Hope this helps
 
Cactus13,

Sorry, I should have paid closer attention to the end of your post. I missed seeing your test routine. You declare array variable Lijst and dimension it to contain 9 elements. The subscript range is 0..8. However, your For Loop construct

Code:
For i=0 to 9
  Lijst(i) = ...
next i

attempts to access array subscript 9 (when i=9), which doesn't exist, resulting in runtime error #9.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor