简体   繁体   English

VBA:运行时错误“ 9”

[英]vba: run time error '9'

When I want to copy the values of cells from one sheet to anoter in 6 Workbooks which are opened (in my case from "Sheet1" cells (B9:E111) to "Sheet2") I'm standing in front of the error: 'run time error 9 : Subscript out of range' this is the code I wrote: 当我想将单元格的值从一张纸复制到打开的6个工作簿中的注释器(在我的情况下,是从“ Sheet1”单元格(B9:E111)到“ Sheet2”),我站在错误的前面:运行时错误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

May someone can help me? 有人可以帮我吗? Thank you 谢谢

"Subscript out of range" means you're accessing an array/collection beyond its boundaries. “下标超出范围”表示您正在访问数组/集合的边界之外的内容。

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

I count several different places in that single instruction that could throw that error. 我在一条指令中数了数个不同的地方,可能会引发该错误。 Split it up. 拆分。

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

The last instruction looks suspicious to me. 最后一条指令对我来说似乎很可疑。 If you're trying to paste Sheet1!B9:E111 into Sheet2!A1 , consider using Copy + PasteSpecial as in Shai Rado's answer . 如果您尝试将Sheet1!B9:E111粘贴到Sheet2!A1 ,请考虑使用Shai Rado的答案中的 Copy + PasteSpecial

If you mean to iterate all open workbooks, consider a For Each loop instead: 如果要迭代所有打开的工作簿,请考虑使用For Each循环:

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

If all of your workbooks are open (the code below can work also if only 1, 2, or 3 are open), it will copy the values from Range("B9:E111") in "Sheet2" and paste them to "Sheet1" from Cell "A1". 如果您所有的工作簿都是打开的(如果仅打开1、2或3,下面的代码也可以工作),它将从"Sheet2" Range("B9:E111")复制值并将其粘贴到“ Sheet1”中来自单元格“ 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 : 编辑1

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

Try this, I think it is a problem to put the value of the whole range into one cell. 试试这个,我认为将整个范围的值放在一个单元格中是一个问题。 I assign it first to an Array and then put the array on from what is A1 + the upper boundaries of the array... 我首先将其分配给一个数组,然后从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

I'll warn that using workbook indexes like this is risky, but try this.. 我警告说,使用这样的工作簿索引是有风险的,但是请尝试这样做。

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

Are you trying to do something like this: 您是否正在尝试执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM