View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Excel Monkey[_2_] Excel Monkey[_2_] is offline
external usenet poster
 
Posts: 36
Default Bubble sort Error (Compile Error: Type Mismtach)

Sorry, one last question. I have converted it to a function but I am not
getting the correct results. I am assuming that I have slipped a position in
the BubbleSort sub. I can figure out where the oversight is. Any ideas?

Sub Test()
Dim x As Variant

ReDim x(0 To 2)

x(0) = 10
x(1) = 7
x(2) = 4

x = BubbleSort(x)
Debug.Print x(0) & ", " & x(1) & ", " & x(2)
End Sub

Private Function BubbleSort(MyArray As Variant) As Variant
Dim First As Integer
Dim Last As Integer
Dim i As Integer
Dim j As Integer
Dim Temp As String
Dim List As String

First = LBound(MyArray)
Last = UBound(MyArray)
For i = First To Last
For j = i + 1 To Last - 1
If MyArray(i) MyArray(j) Then
Temp = MyArray(j)
MyArray(j) = MyArray(i)
MyArray(i) = Temp
End If
Next j
Next i

BubbleSort = MyArray

End Function

"Jim Thomlinson" wrote:

x is single variable of type Variant. Your procedure Bubble sort expects an
array of variants. Just get rid of the brackets

Sub BubbleSort(MyArray As Variant)

--
HTH...

Jim Thomlinson


"Excel Monkey" wrote:

I am having trouble using a Bubble Sort Sub. The issue is in the sub that
calls the sort sub. I keep getting a "Compile Error: Type Mismatch: array
or user defined type expected.". What am I doing wrong?

Sub Test()
Dim x As Variant

ReDim x(0 To 2)

x(0) = 1
x(1) = 10
x(2) = 3

BubbleSort (x)

Sub BubbleSort(MyArray() As Variant)
'Additional Sort code here
End Sub

Thanks

EM