View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Sorting 2D Array

Your forgetting that a 2D array has two dimensions.

Assuming that you are sorting on the leftmost column

BubbleSort2D UnitOfferArray

Function BubbleSort2D(List As Variant)
' Sorts an array using bubble sort algorithm
Dim First As Integer, Last As Integer
Dim i As Integer, j As Integer
Dim Temp As Integer

First = LBound(List,1)
Last = UBound(List,1)
For i = 1 To Last - 1
For j = i + 1 To Last
If List(i,1) List(j,1) Then
for k = 1 to 4
Temp = List(j,k)
List(j,k) = List(i,k)
List(i,k) = Temp
Next k
End If
Next j
Application.StatusBar = "Sorting " & Round(i / Last * 100, 0) & "%"
Next i

End Function

--
Regards,
Tom Ogilvy


ExcelMonkey wrote in message
...
Sorry, I hit send to early


Hi folks. I have been sorting a VBA Array using a bubble sort. It works
fine with when my VBA array is 1-D, but when I change to 2-D it I get a
"Subscript out of range" Error. Is there something obvious I am
forgetting?




ReDim UnitOfferArray(1 To 10, 1 To 4)

For X = 1 to 10
I load the data into the array within this loop
Next X


BubbleSort UnitOfferArray

Function BubbleSort(List As Variant)
' Sorts an array using bubble sort algorithm
Dim First As Integer, Last As Integer
Dim i As Integer, j As Integer
Dim Temp As Integer

First = LBound(List)
Last = UBound(List)
For i = 1 To Last - 1
For j = i + 1 To Last
If List(i) List(j) Then
Temp = List(j)
List(j) = List(i)
List(i) = Temp
End If
Next j
Application.StatusBar = "Sorting " & Round(i / Last * 100, 0) & "%"
Next i

End Function


---
Message posted from http://www.ExcelForum.com/