我有一个在vba中构成ArrayList
的函数。我尝试使用for
循环遍历它,但是我收到错误。我不确定LBound
和UBound
有什么用,我不能在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
?
答案 0 :(得分:3)
收集从0开始,而不是1.这是一个修正
For i = 0 To arr.Count-1
If arr(i) <= 10000 Then 'error
...
End If
Next