在for循环中迭代ArrayList

时间:2018-01-18 09:37:44

标签: excel vba excel-vba

我有一个在vba中构成ArrayList的函数。我尝试使用for循环遍历它,但是我收到错误。我不确定LBoundUBound有什么用,我不能在ArrayList上使用这些功能。

Function test(dataArray() As Double)

    Dim i As Long


    Dim arr As Object
    Set arr = CreateObject("System.Collections.ArrayList")

    For i = LBound(dataArray) To UBound(dataArray)
        arr.Add dataArray(i)
    Next

    arr.Sort

    For i = 1 To arr.Count
       If arr(i) <= 10000 Then 'error
          ...
       End If
    Next

End Function

我在循环中遇到index out of range, index must be positive and should not exceed the collection's size错误。如何在ArrayList周期内无误差地遍历for

1 个答案:

答案 0 :(得分:3)

收集从0开始,而不是1.这是一个修正

For i = 0 To arr.Count-1
       If arr(i) <= 10000 Then 'error
          ...
       End If
    Next