Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Bubble sort Error (Compile Error: Type Mismtach)

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Bubble sort Error (Compile Error: Type Mismtach)

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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Bubble sort Error (Compile Error: Type Mismtach)

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   Report Post  
Posted to microsoft.public.excel.programming
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



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Bubble sort Error (Compile Error: Type Mismtach)

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Bubble sort Error (Compile Error: Type Mismtach)

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Forms-Compile error: user-def type not defined Tita_Guera Excel Programming 1 December 11th 08 07:40 PM
"Compile Error: ByRef argument type mismatch" when calling my function from another module ker_01 Excel Programming 2 August 14th 08 03:53 PM
Help: Compile error: type mismatch: array or user defined type expected lvcha.gouqizi Excel Programming 1 October 31st 05 08:20 PM
Compile error: type mismatch pjhageman Excel Programming 2 January 10th 04 08:48 PM


All times are GMT +1. The time now is 02:45 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"