VBA数组添加

时间:2016-06-27 19:32:58

标签: excel vba excel-vba macros

 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次的任何建议?

1 个答案:

答案 0 :(得分:0)

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