Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
No end sub after first block of code?
-- Regards, Nigel "Excel Monkey" wrote in message ... 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 |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Use
Sub BubbleSort(MyArray As Variant) and not Sub BubbleSort(MyArray() As Variant) HTH, Bernie MS Excel MVP "Excel Monkey" wrote in message ... 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 |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Excel Monkey,
If the data that you are sorting is a combination of Alpha and Numeric of just numeric and you have 1, 2, ....,10,..20,..30 you will get 1,10,2,20,3,30. I had the same thing. Change the line: If MyArray(i) MyArray(j) Then TO If Val(MyArray(i)) Val(MyArray(j)) Then HTH Harry Excel Monkey wrote: 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 |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Your biggest mistake was dimming Temp as a string, but you also don't need
to double increment through the indices. 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 Variant Dim Switched As Boolean Switched = True First = LBound(MyArray) Last = UBound(MyArray) While Switched Switched = False For i = First To Last - 1 j = i + 1 If MyArray(i) MyArray(j) Then Temp = MyArray(j) MyArray(j) = MyArray(i) MyArray(i) = Temp Switched = True End If Next i Wend BubbleSort = MyArray End Function HTH, Bernie MS Excel MVP "Excel Monkey" wrote in message ... 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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Forms-Compile error: user-def type not defined | Excel Programming | |||
"Compile Error: ByRef argument type mismatch" when calling my function from another module | Excel Programming | |||
Help: Compile error: type mismatch: array or user defined type expected | Excel Programming | |||
Compile error: type mismatch | Excel Programming |