循环索引混乱

Loop index confusion

我试图通过为工作簿中的所有工作表创建一个数组,该数组填充了所述单元格引用中的值,从而添加外部工作簿中位于同一单元格引用中的任何十二个连续工作表中的值。然后,使用 for 循环,我将 select 十二个最近的值,并将它们添加到打开的工作簿上的一个单元格中。下面的代码适用于此目的,但我只是试图在打开的工作簿上一次用总和填充十二个单元格。目前,我正在填充十三个单元格而不是十二个单元格。我不明白这是为什么,但它可能与 for 循环中的代码或 for 循环索引有关。

代码:

Sub LoopValues()
    Application.ScreenUpdating = False  'prevents premature updates during calculation
    
    ' Define vars
    Dim wkbOpen As Excel.Workbook
    Dim wkbRef As Excel.Workbook
 
    Dim ws As Worksheet
    Dim wsName As String
    Dim wsExists As Boolean

    Dim counter As Integer
    Dim i As Integer
    
    ' set workbooks
    Set wkbOpen = ActiveWorkbook
    Set wkbRef = Application.Workbooks.Open("FileName.xlsx")
   
   
   ' creates a string for each sheet in FileName
        ' Formatting: (year, month, day) --> mmyy (disregards day)
        ' Start year: 2020
        ' Start month: Feb
        ' Start mmyy: 0220
    wsName = Format(DateSerial(2020, 2 + counter, 1), "mmyy")
    
    ' Set Counter
    counter = 0
    
    ' condition for do while to run
    wsExists = True
    
    ' declare array for values from sheets
    Dim arr() As Variant
    
    ' create an empty array to store FileName values, and alter in Do While (re dim)
    arr = Array()
    
    Do While wsExists
        
        'create a string for each sheet
         wsName = Format(DateSerial(2020, 2 + counter, 1), "mmyy")
         
        On Error Resume Next
        ' try to set the ws to the name of the current sheet in BatchEmissions
        Set ws = wkbRef.Sheets(wsName)
        
        ' if sheets(wsName) does not exists Err.Number <> 0
        If Err.Number <> 0 Then
            wsExists = False
            On Error GoTo 0
            
        Else
            
            ' resize array (increase bound by one) and ensure previous data is saved
            ReDim Preserve arr(UBound(arr) + 1)
            
            ' update array to inlcude the sheet in FileName's value in AA208
            arr(UBound(arr)) = ws.Range("AA208").Value
        
        End If
            ' increments to consecutive sheet (mmyy) - (counter changes month value)
            counter = counter + 1
    
    Loop
    
    ' close FileName
    wkbRef.Close False
    
    Dim newLowerBound As Integer
    newLowerBound = UBound(arr) - 12
    
    Dim temp As Integer
    
    temp = 0
    
    ' loop through the last twelve values in the array
    For i = newLowerBound To UBound(arr)
    
        
        ' set cell ref to value
        ' to sum arr(0) to arr(11) and increment by i each time
        ' we must transpose the array, and wrap the transposed array inside an index
        ' and for ROWS generate an array each time incremented by i through Evaluate(ROW(1:12) etc
        wkbOpen.Sheets("Site").Cells(99 + temp, "Q").Value = Application.Sum(Application.Index(Application.Transpose(arr), Evaluate("Row(" & (1 + temp) & ":" & (12 + temp) & ")"), 0))

        temp = temp + 1
    
    Next i
    
    Application.ScreenUpdating = True

End Sub

这个:newLowerBound = UBound(arr) - 12

对此:newLowerBound = UBound(arr) - 11