0
当我试图让Excel选择使用哪个工作簿时,它给我一个下标超出范围的错误。我不知道我是否必须引用它来自哪个文件夹。他们都在同一个文件夹中。我研究过其他人的解决方案,但我没有相同的格式和任务。该错误是在那里分配工作簿的数字当选择工作簿时下标超出范围
Sub bringbookstogether()
Dim currentsheet As Worksheet
Set currentsheet = Application.ActiveSheet
Dim othersheets As Worksheet
Dim wbook As Workbook
Dim c As String
'assigns the number to start with
Dim a, b, d As Integer
a = 4
b = 6
d = 1
'assigns workbook numbers
If (d = 1) Then
Set wbook = Workbooks("MaintPrep Sheet 1st")
Else
If (d = 2) Then
Set wbook = Workbooks("MaintPrep Sheet 2nd")
Else
If (d = 3) Then
Set wbook = Workbooks("MaintPrep Sheet 3rd")
End If
End If
End If
'End if it's done with all the workbooks
Do Until (d = 4)
'Looks for the sheet that has the same name
Do While (c = currentsheet.Name)
'Ends in row 99
Do While (b < 99)
'Ends in Column 52
Do While (a < 52)
currentsheet.Cells(b, a) = currentsheet.Cells(b, a) + Workbooks(d).Sheets(c).Cells(b, a)
a = a + 1
Loop
b = b + 1
Loop
Loop
d = d + 1
Loop
End Sub
我必须做一个对每个工作簿? – MaxAttack102
取决于您的用例。你也可以假设默认关闭工作簿,并在设置wbook变量时使用'Workbooks.Open(“path \ filename.xlsx”)'。 –
没有错误,谢谢。 – MaxAttack102