vba:运行时错误' 9'

时间:2016-12-06 15:00:05

标签: excel vba excel-vba

当我想将单元格的值从一张纸张复制到打开的6张工作簿中时(在我的情况下从" Sheet1"单元格(B9:E111)到" Sheet2&#34) ;)我站在错误的前面:'运行时错误9:下标超出范围' 这是我写的代码:

Sub sbCopyRangeToAnotherSheet()

  For i = 1 To 6

     Workbooks(i).Worksheets("Sheet2").Range("A1").Value = Workbooks(i).Worksheets("Sheet1").Range("B9:E111").Value  

  Next i

End Sub
是的,有人可以帮助我吗? 谢谢

5 个答案:

答案 0 :(得分:5)

“下标超出范围”意味着您正在访问超出其边界的数组/集合。

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,请考虑使用Shai Rado's answer中的Copy + PasteSpecial

如果您要迭代所有打开的工作簿,请考虑使用For Each循环:

Dim book As Workbook
For Each book In Workbooks
    '...
Next

答案 1 :(得分:2)

如果您的所有工作簿都已打开(如果只打开1,2或3,则下面的代码也可以使用),它会复制Range("B9:E111")"Sheet2"的值并将其粘贴到"工作表Sheet"来自Cell" A1"。

Sub sbCopyRangeToAnotherSheet()

Dim i As Integer
Dim wb() As Workbook

' work with dynamic number of current open workbooks
ReDim wb(1 To Application.Workbooks.count)

For i = 1 To Application.Workbooks.count
    Set wb(i) = Workbooks(i)
    wb(i).Worksheets("Sheet1").Range("B9:E111").Copy
    wb(i).Worksheets("Sheet2").Range("A1").PasteSpecial xlValues
Next i

End Sub

<强> EDIT1

Sub sbCopyRangeToAnotherSheet()

Dim i As Integer
Dim wb() As Workbook

' work with dynamic number of current open workbooks
ReDim wb(1 To Application.Workbooks.Count)

For i = 1 To Application.Workbooks.Count
    Set wb(i) = Workbooks(i)
    wb(i).Worksheets(1).Range("B9:E111").Copy
    wb(i).Worksheets(2).Range("A1").PasteSpecial xlValues
Next i

End Sub

答案 2 :(得分:1)

试试这个,我认为将整个范围的值放入一个单元格是个问题。我首先将它分配给一个数组,然后将数组从A1 +数组的上边界放入......

Sub sbCopyRangeToAnotherSheet()

Dim vArr() As Variant

  For i = 1 To 6

     vArr = Workbooks(i).Worksheets("Sheet1").Range("B9:E111").Value
     With Workbooks(i).Worksheets("Sheet2")
        .Range(.Cells(1, 1), .Cells(UBound(vArr, 1), UBound(vArr, 2))).Value = vArr
     End With
  Next i

End Sub

答案 3 :(得分:1)

我会警告说使用像这样的工作簿索引是有风险的,但试试这个......

Sub sbCopyRangeToAnotherSheet()

   For i = 1 To 6
       Workbooks(i).Worksheets("Sheet1").Range("B9:E111").Copy Workbooks(i).Worksheets("Sheet2").Range("A1")
   Next i

End Sub

答案 4 :(得分:0)

你是否想要做这样的事情:

Sub sbCopyRangeToAnotherSheet()

  For i = 1 To 6

     Workbooks(i).Worksheets("Sheet2").Range("A1").Value = worksheetfunction.sum(Workbooks(i).Worksheets("Sheet1").Range("B9:E111"))

  Next i

End Sub