View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Tim Kredlo Tim Kredlo is offline
external usenet poster
 
Posts: 11
Default referencing/sorting arrays

List members,

Please help!

In module1 of my project I have declared:
Public aryLength(1 To 12) As Integer

In the 'change' function for a listbox on a user form I fill this array with
values retrieved from a worksheet.

I then call my subprocedure SortArrayAscend (which is in module1) as follows:
SortArrayAscendArray (aryLength)

The code for SortArrayAscend is:
Public Sub SortArrayAscend(aryLength)
'Sort an array in ascending order

Dim Index01 As Integer
Dim Index02 As Integer
Dim Work As Integer

For Index01 = LBound(aryLength) To UBound(aryLength) - 1
For Index02 = Index01 + 1 To UBound(aryLength)
If CInt(aryLength(Index01)) CInt(aryLength(Index02)) Then
Work = CInt(aryLength(Index02))
aryLength(Index02) = CInt(aryLength(Index01))
aryLength(Index01) = Work
End If
Next Index02
Next Index01
End Sub

If the elements in aryLength are "16, 18, 20, 12, 8, 14, 10, 0, 0, 0, 0, 0,
0" when passed to SortArrayAscend , when I debug SortArrayAscend, at "End
Sub" time , aryLength is "0, 0, 0, 0, 0, 0, 8, 10, 12, 14, 16, 18, 20" which
is what I expect.

However, at the very next statement, after returning from SortArrayAscend,
aryLength is exactly as it was before sorting. Since I am passing 'by
reference' I do not understand why the array is not sorted.

(I had initially attempted to write 'SortArrayAscend' as a 'generic' routine
to accept dynamic arrays, but kept getting 'type mismathes', but that's
another story. However, any help here would also be appreciated!)

Could someone please help me to understand:
1) Why what I am doing does not work as I expect (Having declared aryLength
as a 'public' variable and attempting to pass by reference.)
2) What needs to be done to get it to work.

Thanks in advance for any help.