数组的排序过程不起作用 - 键入不匹配

时间:2013-05-06 11:24:13

标签: vba sorting excel-vba excel

我正在研究以下代码,它应该将电子表格中的值转换为数组,对它们进行排序(这里是三重排序 - 一次排序三个数组),最后将结果放在另一个表格中。 ..

问题是我收到了一个超出范围的“下标”错误消息,我真的不知道如何修复它 我似乎有这个问题每次我尝试排序一个数组..所以排序一定有问题......(这里称为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 个答案:

答案 0 :(得分: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)

因为你不能在字符串变量中加入Double值。

更新2 您已经明确了Temp3 as Double但是您正在传递Integer数据类型。在我的计算机上(Win 7 XP / Excel 2010),这不会导致错误,因为可以将Integer传递给Double变量。我怀疑Excel for Mac中有一个怪癖,它不允许这种行为。

此版本在我的计算机上执行时没有错误,并且对Temp2Temp3 的数据类型进行了更改:

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