×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Error 9: Subject out of range using QuickSort routine

Error 9: Subject out of range using QuickSort routine

Error 9: Subject out of range using QuickSort routine

(OP)
Hello,

I'm trying to sort a list of data and found the following code (for a Quick Sort routine) on the devx domain (http://www.devx.com/premier/mgznarch/vbpj/2000/04apr00/cs0400/cs0400.asp) 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

RE: Error 9: Subject out of range using QuickSort routine

Cactus13,

Runtime error 9, "Subscript out of range", 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 "Option Base 1" is placed at the top of your code module.  Given that you probably copied & pasted the Quicksort code, this sounds like an "off by one" 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

RE: Error 9: Subject out of range using QuickSort routine

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

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

White Paper - How ESI is Helping Move New Medical Device Product to Market Quicker & More Cost Effic
Early Supplier Involvement has long been a strategy employed by manufacturers to produce innovative products. Now, it almost seems like a necessity. Because decisions made in the design phase can positively affect product quality and costs, this can help add value to OEM bottom lines. This white paper will discuss many facets of ESI, including why it’s so valuable today, what challenges limit the benefits of ESI, how cost is impacted, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close