Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50057

Help needed with a sorting procedure on an array - Subscript out of range

$
0
0
I am working on the following code, which is supposed to get the values from a spreadsheet into arrays, sort them (it's a triple sort here - three arrays are sorted at once), and finally place the results in another sheet...

The problem is I get a Subscript out of range" error message and I really don't know how to fix it :(
I seem to have this problem EACH time I try sorting an array.. so there must be something wrong with the Sort ... (it is called TriFonds here)

Any help will be greatly appreciated..

Code:

Option Explicit
Option Base 1

Sub Class()
    Dim i As Integer, j As Integer, k As Integer
   
    Dim nb_Actions As Long
 
   
    ReDim NomAction(nb_Actions) As Double
    ReDim IndiceAction(nb_Actions) As Double
    ReDim Ratio(nb_Actions) As Double

   
   
    With Worksheets("Actions")
        nb_Actions = .Cells(1, Columns.Count).End(xlToLeft).Column
    End With
   
    With Worksheets("Actions")
    'I fill in arrays with data from the column
            For i = 1 To nb_Actions
                Ratio(i) = .Cells(1 + i, 10)
            Next i
   
 
            For j = 1 To nb_Actions
                IndiceAction(j) = .Cells(1 + j, 11)
            Next j
           

            For k = 1 To nb_Actions
                NomAction(k) = .Cells(1 + k, 9)
            Next k
    End With
           
            Call TriFonds(Ratio(), NomAction(), IndiceAction())
           
 
   
    With Worksheets("Performance")
        For i = 1 To nb_Actions
            .Cells(4 + i, 2) = IndiceAction(i)
            .Cells(4 + i, 3) = NomAction(i)
            .Cells(4 + i, 4) = Ratio(i)
        Next i
    End With
End Sub

Sub TriFonds(Tab1() As Double, Tab2() As Double, Tab3() As Double)
    Dim Temp1 As Double
    Dim Temp2 As Double
    Dim Temp3 As Double
    Dimi As Long, j As Long
    Dim ligne_Fin As Long
   
    'Last line from the sorting procedure
    ligne_Fin = UBound(Tab1)
   
    For i = 2 To ligne_Fin
        Temp1 = Tab1(i)
        Temp2 = Tab2(i)
        Temp3 = Tab3(i)
       
        For j = i - 1 To 1 Step -1 'Increasing order
            If (Tab1(j) <= Temp1) Then GoTo 10
                Tab1(j + 1) = Tab1(j)
                Tab2(j + 1) = Tab2(j)
                Tab3(j + 1) = Tab3(j)
            Next j
        j = 0
       
       
10    Tab1(j + 1) = Temp1
        Tab2(j + 1) = Temp2
        Tab3(j + 1) = Temp3
    Next j
Next i
   
End Sub

Attached Files

Viewing all articles
Browse latest Browse all 50057

Trending Articles