I have a bubble sort function. The code works inside the sort function sub. I can see that the array UnsortedArray is populated and the bubble sort works using the code:
Debug.Print LBound(UnsortedArray, 1) & "/" & UBound(UnsortedArray, 1) - 1
Immediate Window = "1/9"
However after exiting the sort function sub, the code fails with a Type Mismatch Error (13) - SortedArray is "Empty" on the line of code below:
Debug.Print LBound(SortedArray, 1) & "/" & UBound(SortedArray, 1) - 1
What this tells me is that I am not transferring the sorted array arising from the sort function sub properly in the line of code:
SortedArray = BubbleSort(MonthDayArray, sortcolumn)
Why is this?
*************
Option Base 1
Sub Test()
Dim MonthDayArray As Variant
Dim SortedArray As Variant
Dim sortcolumn As Integer
Dim x as Integer
'Code to fill MonthDayArray
'Send unsorted array to sort function
sortcolumn = 4
SortedArray = BubbleSort(MonthDayArray, sortcolumn)
'Test SortedArray has content
Debug.Print LBound(SortedArray, 1) & "/" & UBound(SortedArray, 1) - 1
End Sub
*****************
Public Function BubbleSort(UnsortedArray As Variant, keyColumn As Integer, Optional SortDescending As Boolean) As Variant
Dim i As Integer, j As Integer
Dim v As Integer
Dim t As Integer
For i = LBound(UnsortedArray, 1) To UBound(UnsortedArray, 1) - 1
For j = i + 1 To UBound(UnsortedArray, 1)
If UnsortedArray(j, keyColumn) > UnsortedArray(i, keyColumn) Then
t = UnsortedArray(i, 1)
v = UnsortedArray(i, keyColumn)
UnsortedArray(i, 1) = UnsortedArray(j, 1)
UnsortedArray(i, keyColumn) = UnsortedArray(j, keyColumn)
UnsortedArray(j, 1) = t
UnsortedArray(j, keyColumn) = v
End If
Next j
Next i
'Test Contents of UnsortedArray
Debug.Print LBound(UnsortedArray, 1) & "/" & UBound(UnsortedArray, 1) - 1
End Function
Debug.Print LBound(UnsortedArray, 1) & "/" & UBound(UnsortedArray, 1) - 1
Immediate Window = "1/9"
However after exiting the sort function sub, the code fails with a Type Mismatch Error (13) - SortedArray is "Empty" on the line of code below:
Debug.Print LBound(SortedArray, 1) & "/" & UBound(SortedArray, 1) - 1
What this tells me is that I am not transferring the sorted array arising from the sort function sub properly in the line of code:
SortedArray = BubbleSort(MonthDayArray, sortcolumn)
Why is this?
*************
Option Base 1
Sub Test()
Dim MonthDayArray As Variant
Dim SortedArray As Variant
Dim sortcolumn As Integer
Dim x as Integer
'Code to fill MonthDayArray
'Send unsorted array to sort function
sortcolumn = 4
SortedArray = BubbleSort(MonthDayArray, sortcolumn)
'Test SortedArray has content
Debug.Print LBound(SortedArray, 1) & "/" & UBound(SortedArray, 1) - 1
End Sub
*****************
Public Function BubbleSort(UnsortedArray As Variant, keyColumn As Integer, Optional SortDescending As Boolean) As Variant
Dim i As Integer, j As Integer
Dim v As Integer
Dim t As Integer
For i = LBound(UnsortedArray, 1) To UBound(UnsortedArray, 1) - 1
For j = i + 1 To UBound(UnsortedArray, 1)
If UnsortedArray(j, keyColumn) > UnsortedArray(i, keyColumn) Then
t = UnsortedArray(i, 1)
v = UnsortedArray(i, keyColumn)
UnsortedArray(i, 1) = UnsortedArray(j, 1)
UnsortedArray(i, keyColumn) = UnsortedArray(j, keyColumn)
UnsortedArray(j, 1) = t
UnsortedArray(j, keyColumn) = v
End If
Next j
Next i
'Test Contents of UnsortedArray
Debug.Print LBound(UnsortedArray, 1) & "/" & UBound(UnsortedArray, 1) - 1
End Function