2013-05-06 26 views
0

我正在處理下面的代碼,它應該從電子表格中獲取值到數組中,對它們進行排序(這裏是一個三重排序 - 三個數組排序在一次),並最後把結果放在另一個表...數組的排序過程不起作用 - 類型不匹配

問題是我得到一個下標超出範圍「的錯誤消息,我真的不知道如何解決它 我似乎有這個問題每個我試着對一個數組進行排序..所以必須有排序錯誤...(這裏叫做TriFonds)

任何幫助將不勝感激..

Option Explicit 
Option Base 1 

Sub Class() 
Dim i As Integer, j As Integer, k As Integer 

Dim nb_Actions As Long 


With Worksheets("Actions") 
    nb_Actions = .Cells(1, Columns.Count).End(xlToLeft).Column 
End With 

ReDim NomAction(nb_Actions) As Double 
ReDim IndiceAction(nb_Actions) As Double 
ReDim Ratio(nb_Actions) As Double 

With Worksheets("Actions") 
'I fill in arrays with data from the column 
     For i = 1 To nb_Actions 
      Ratio(i) = .Cells(18 + i, 2).Value 
     Next i 


     For j = 1 To nb_Actions 
      IndiceAction(j) = .Cells(18 + j, 3).Value 
     Next j 


     For k = 1 To nb_Actions 
      NomAction(k) = .Cells(18 + k, 1).Value 
     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 
Dim i 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) 

    j = 0 


10 Tab1(j + 1) = Temp1 
    Tab2(j + 1) = Temp2 
    Tab3(j + 1) = Temp3 
Next j 
Next i 

End Sub 

回答

1

當您重新設置數組變量時,nb_Actions的值爲零。所以,你已經聲明瞭基數爲零的數組變量,然後你去爲它們賦值,從For i = 1 to ...開始,這將導致超範圍類型錯誤。

移動這些行:

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 

更新

下載文件後,我確定在TriFonds子程序不匹配聲明。

您已聲明Temp2 as Double,但您試圖將字符串值從「Tab2()As String」)分配給此變量。這是造成不匹配:

Temp2 = Tab2(i)

,因爲你不能在一個字符串變量放一個雙精度值。

更新2您已經提取Temp3 as Double但您傳遞的是Integer數據類型。在我的電腦上(Win 7 XP/Excel 2010),這不會導致錯誤,因爲Integer可以傳遞給Double變量。我懷疑在Mac的Excel中存在一個不允許這種行爲的怪癖。

該版本沒有錯誤執行我的電腦上,並結合變化的Temp2數據類型和Temp3

Option Explicit 
Option Base 1 

Sub Class() 
    Dim i As Integer, j As Integer, k As Integer 

    Dim nb_Actions As Long 

    With Worksheets("Actions") 
     nb_Actions = .Cells(1, Columns.Count).End(xlToLeft).Column 
    End With 

    ReDim NomAction(nb_Actions) As String 
    ReDim IndiceAction(nb_Actions) As Integer 
    ReDim Ratio(nb_Actions) As Double 

    With Worksheets("Actions") 
    'I fill in arrays with data from the column 
      For i = 1 To nb_Actions 
       Ratio(i) = .Cells(18 + i, 2) 
      Next i 


      For j = 1 To nb_Actions 
       IndiceAction(j) = .Cells(18 + j, 3) 
      Next j 


      For k = 1 To nb_Actions 
       NomAction(k) = .Cells(18 + k, 1) 
      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 String, Tab3() As Integer) 
    Dim Temp1 As Double 
    Dim Temp2 As String '## changed data type ## 
    Dim Temp3 As Integer '## changed data type ## 
    Dim i 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) 

     j = 0 


10  Tab1(j + 1) = Temp1 
     Tab2(j + 1) = Temp2 
     Tab3(j + 1) = Temp3 
    Next j 
Next i 

End Sub 
+0

我更新的代碼,但我仍然得到同樣的錯誤:( – seigna 2013-05-06 11:38:02

+0

請註明線給你錯誤 – 2013-05-06 11:38:43

+0

我該怎麼做? 順便說一句,不會有任何錯誤,現在它只是返回零到處:( – seigna 2013-05-06 11:47:32