Given the following Sub, why is the sorted array (my output) is out of the Range? How can I read the elements of the sorted array?
Thank you.
B.
The sorting function is
Thank you.
B.
Code:
Sub Sorttest()
Dim k As Long
Dim N As Long
Dim S As String
Dim InArray() As Variant
Dim OuArray() As Variant
Dim SumIO() As Variant
ReDim InArray(10)
ReDim OuArray(10)
ReDim SumIO(10)
Sheets("Examples").Activate
With Range("B2")
For k = 1 To 10
InArray(k) = .Cells(1, k).Value
Next k
End With
OuArray = InsertSort(InArray)
MsgBox LBound(OuArray)
MsgBox UBound(OuArray)
'MsgBox OuArray(1)
For k = 1 To 10
SumIO(k) = OuArray(k) + InArray(k)
Next k
With Range("B5")
For k = 1 To 10
.Cells(k, 1).Value = OuArray(k)
Next k
End With
End Sub
The sorting function is
Code:
Public Function InsertSort(Array_Values As Variant) As Variant
'
Application.Calculation = xlCalculationManual
Application.Volatile (False)
Application.ScreenUpdating = False
'Dim InsertSort() As Double
Dim nums() As Double
Dim limit As Long
Dim i As Long, j As Long
Dim num_greater
Dim new_array() As Double
Dim base_variable As Double
Dim Rank As Long
limit = UBound(Array_Values)
ReDim Preserve nums(1 To limit)
ReDim Preserve new_array(1 To limit)
For i = 1 To limit
nums(i) = Array_Values(i)
Next i
For i = 1 To limit
num_greater = 0
base_variable = nums(i)
For j = 1 To limit
If base_variable < nums(j) Then
num_greater = num_greater + 1
End If
Next j
Rank = limit - num_greater
new_array(Rank) = nums(i)
Next i
InsertSort = WorksheetFunction.Transpose(new_array)
Application.Volatile (True)
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Function