添加VBA数组


 For j = 1 To 8
 Sheet5.Cells(j + 1, 2) = 480
 Next
 t = 0
 c = 0
     For j = LBound(arrayTime) + 1 To UBound(arrayTime)
        MsgBox "j " & j
       'MsgBox (t)
        numMins = Sheet5.Cells((j + 1) - (8 * c), 2) - arrayTime(j)
        If numMins < 0 Then
        t = t + 1
        ReDim Preserve arrayTime(numrows - 1 + t)
        arrayTime(numrows - 1 + t) = arrayTime(j)
        MsgBox (arrayTime(numrows - 1 + t))
        Else
        Sheet5.Cells((j + 1) - (8 * c), 2) = numMins
        End If
        If j = 8 * (c + 1) Then
        c = c + 1
        End If
        MsgBox ("end " & t)
    Next

如果条件为真,我试图添加一个值到arrayTime。我成功地添加了它,但是for循环不会通过添加的元素重新维度循环。数组最初包含12个元素,然后我添加了第13个元素,但是循环没有识别第13个元素,只循环了12次。关于如何让for循环循环13次有什么建议吗?

添加一个循环计数器,例如i,并将其设置为LBound(arrayTime) + 1,然后使用Do Until (i = UBound(arrayTime))。这将强制VBA在每次循环之前重新计算上限。

最新更新