下标超出范围印刷单元

时间:2017-07-03 20:06:26

标签: excel vba excel-vba

我的问题是细胞开始在下面的行上添加的部分

currentsheet.Cells(b, a) = currentsheet.Cells(b, a) + Workbooks(d).Sheets(c).Cells(b, a)

它抛出了我

  

下标超出范围

我的代码应该从三个不同的工作簿中的多个图表中获取数字,并将其放入另一个工作簿中的一个主工作表中,同时添加每个图表中的所有数字。所有工作表的名称和格式都相似。

Sub bringbookstogether()

Dim currentsheet As Worksheet
Set currentsheet = Application.ActiveSheet

'assigns the number to start with
Dim a, b, c, d As Integer

a = 4
b = 6
c = 3
d = 1

Dim wsheet As Worksheet
Set wsheet = Application.ActiveWorkbook.Sheets(c)

Dim wbook As Workbook

'assigns workbook numbers
If (d = 1) Then
    Set wbook = Workbooks.Open("C:\Users\mminchuk\Documents\Updated MaintPrep Sheets\MaintPrep Sheet 1st.xlsm", UpdateLinks:=xlUpdateLinksAlways)
    Else
    If (d = 2) Then
        Set wbook = Workbooks.Open("C:\Users\mminchuk\Documents\Updated MaintPrep Sheets\MaintPrep Sheet 2nd.xlsm", UpdateLinks:=xlUpdateLinksAlways)
        Else
        If (d = 3) Then
            Set wbook = Workbooks.Open("C:\Users\mminchuk\Documents\Updated MaintPrep Sheets\MaintPrep Sheet 3rd.xlsm", UpdateLinks:=xlUpdateLinksAlways)
        End If
    End If
End If

Application.ScreenUpdating = False

'End if it's done with all the workbooks

Do Until (d = 4)

'Looks for the sheet that has the same name
If (wsheet.Name = currentsheet.Name) Then


    currentsheet.Cells(b, a) = currentsheet.Cells(b, a) + Workbooks(d).Sheets(c).Cells(b, a)
    a = a + 1
    b = b + 1

    If a = 51 Then

        If b = 99 Then

        End If
    End If
End If


d = d + 1
Loop

Application.ScreenUpdating = True

End Sub

0 个答案:

没有答案