「下標越界」意味着你訪問數組/集合超越其界限。
Workbooks(i).Worksheets("Sheet2").Range("A1").Value = Workbooks(i).Worksheets("Sheet1").Range("B9:E111").Value
我在單指令中計算了幾個不同的地方,可能會引發這個錯誤。拆分它。
Dim book As Workbook
' if it blows up here, check how many books you have open:
Set book = Workbooks(i) 'consider looping from 1 To Workbooks.Count instead of 1 To 6
Dim source As Worksheet
' if it blows up here, check the filename of the book and whether it has a "Sheet1":
Set source = book.Worksheets("Sheet1")
Dim destination As Worksheet
' if it blows up here, check the filename of the book and whether it has a "Sheet2":
Set destination = book.Worksheets("Sheet2")
' when it blows up here, consider exactly what you're trying to do:
destination.Range("A1").Value = source.Range("B9:E111").Value
最後一條指令對我來說看起來很可疑。如果您嘗試將Sheet1!B9:E111
粘貼到Sheet2!A1
中,請考慮使用Copy
+ PasteSpecial
,如Shai Rado's answer中所示。
如果你的意思是遍歷所有打開的工作簿,考慮For Each
循環,而不是:
Dim book As Workbook
For Each book In Workbooks
'...
Next
您正試圖將多個單元格複製到一個單元格中。也許嘗試從兩端刪除.value?或者讓陳述的左半部分的範圍與右側的大小相同? – Rdster
你有6個工作簿嗎?他們是否都有「Sheet2」和「Sheet1」?你也試圖把許多單元格的價值合而爲一。 –
Rdster-我試圖把相同的範圍(A1:D103),但同樣的錯誤。 –