I've tried about every combination I could think of but no dice...
This is the original code coming from Microsoft's website and out of the box it works. So I thought, let's try to make it work for me.
Function BubbleSort(TempArray As Variant)
Dim Temp As Variant
Dim i As Integer
Dim NoExchanges As Integer
' Loop until no more "exchanges" are made.
Do
NoExchanges = True
' Loop through each element in the array.
For i = 1 To UBound(TempArray) - 1
' If the element is greater than the element
' following it, exchange the two elements.
If TempArray(i) > TempArray(i + 1) Then
NoExchanges = False
Temp = TempArray(i)
TempArray(i) = TempArray(i + 1)
TempArray(i + 1) = Temp
End If
Next i
Loop While Not (NoExchanges)
End Function
Sub BubbleSortMyArray()
Dim TheArray As Variant
' Create the array.
TheArray = Array(15, 8, 11, 7, 33, 4, 46, 19, 20, 27, 43, 25, 36)
' Sort the Array and display the values in order.
BubbleSort TheArray
For i = 1 To UBound(TheArray)
MsgBox TheArray(i)
Next i
End Sub
This I what I changed it into to adapt to my needs:
Function BubbleSort(TempArray As Long)
Dim Temp As Long
Dim i As Integer
Dim NoExchanges As Integer
' Loop until no more "exchanges" are made.
Do
NoExchanges = True
' Loop through each element in the array.
For i = 1 To UBound(TempArray) - 1
' If the element is greater than the element
' following it, exchange the two elements.
If TempArray(i) > TempArray(i + 1) Then
NoExchanges = False
Temp = TempArray(i)
TempArray(i) = TempArray(i + 1)
TempArray(i + 1) = Temp
End If
Next i
Loop While Not (NoExchanges)
End Function
Sub BubbleSortMyArray()
Dim TheArray(1 To 10) As Long
' Create the array.
TheArray(1) = 100
TheArray(2) = 200
TheArray(3) = 40
' Sort the Array and display the values in order.
BubbleSort TheArray
For i = 1 To UBound(TheArray)
MsgBox TheArray(i)
Next i
End Sub
That get's me the Error 'Byref argument types do not match'.
So I tried calling it like this:
Which gets me the Error 'Matrix argument must be by ref. '
Also what goes beyond me is how True can be assigned to the NoExchanges integer instead of a boolean. I do understand how bubblesort works, I just can get it to sort my array. Sorry for being such a noob...
Bookmarks