Sorting an Array - problem with Subscript out of Range

Beryo

New Member
Joined
Jan 9, 2011
Messages
13
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.

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
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
OuArray is a 2D (2 dimensional) array.

<font face=Courier New><SPAN style="color:#00007F">For</SPAN> k = 1 <SPAN style="color:#00007F">To</SPAN> 10<br>     SumIO(k) = OuArray(k, 1) + InArray(k)<br><SPAN style="color:#00007F">Next</SPAN> k<br>    <br><SPAN style="color:#00007F">With</SPAN> Range("B5")<br>    <SPAN style="color:#00007F">For</SPAN> k = 1 <SPAN style="color:#00007F">To</SPAN> 10<br>        MsgBox .Cells(k, 1).Address<br>         .Cells(k, 1).Value = OuArray(k, 1)<br>    <SPAN style="color:#00007F">Next</SPAN> k<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN></FONT>

...so you must include both dimensions when accessing or assigning a value.

I did not read the code very carefully, but it appears to me that unless you have Option Base set at 1 (which I personally do not use), then the first element of your arrays is empty.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top