Excel 2013 VBA: 下标超出范围(错误 9)

Excel 2013 VBA: Subscript out of Range (Error 9)

所以我有这个代码:

Sub CopyItems()
    Dim Source As String
    Dim Target As String

    'Dim SourceSheet As String
    'Dim TargetSheet As String

    Source = "Source.xlsm"
    Target = "needChange.xlsm"

    'SourceSheet = "Sprint backlog"
    'TargetSheet = "Sheet1"

    Workbooks(Source).Sheets("Sprint backlog").Range("B6:B15").Copy
    Workbooks(Target).Sheets("Sheet1").Range("A14:A23").Paste '<-ERROR here
End Sub

它给了我标题中表示的 Run-time 错误“9”。代码是如此简单,以至于我完全被难住了。 我在网上看了看,似乎是因为名称不存在,但是 sheet 和工作簿都存在,而且名称相同。任何代码之间都没有 space 或奇怪的字符。

基本上,我想将 Source.xlsm 中的 sheet "Sprint backlog" 范围从 B6 到 B15 的列复制到 needChange.xlsm 的 Sheet1 中的范围 A14 到 A23

我试过了,没有成功:

Workbooks(Source).Sheets("Sprint backlog").Range("B6:B15").Copy _
Workbooks(Target).Sheets("Sheet1").Range("A14:A23").PasteSpecial

并且还修改了代码,现在注释掉了。

我怀疑宏无法访问目标文件 (needChange.xlsm),因为它找不到或无法访问它,因此 return 问题,但我想不通了解如何使用代码修复它..

如果它有帮助,而 运行 宏,此代码中的两个工作簿对我来说都是打开和访问的。

我正在向您寻求帮助。

非常感谢。 最好的问候。

这比预期的要棘手。我大量借鉴了这个网页http://ccm.net/faq/24666-excel-vba-copy-data-to-another-workbook

我必须添加对工作表的引用以进行复制和粘贴才能使其正常工作。

发布的代码要求两个工作簿都打开,但是如果给 wbTarget 一个路径名,则可以打开它。在这种情况下,您可以注释掉出现在 -OR-.

之后的两行

该代码还可以保存和关闭目标工作簿。

Sub CopyOpenItems()
   '
   ' CopyOpenItems Macro
   ' Copy open items to sheet.
   '
   ' Keyboard Shortcut: Ctrl+Shift+O
   '
   Dim wbTarget            As Workbook 'workbook where the data is to be pasted
   Dim wbThis              As Workbook 'workbook from where the data is to copied
   Dim strName             As String   'name of the source sheet/ target workbook

   'set to the current active workbook (the source book)
   Set wbThis = ActiveWorkbook

   'get the active sheetname of the book
   strName = ActiveSheet.Name

   'open a workbook that has same name as the sheet name
   'Set wbTarget = Workbooks.Open("C:\YourPath\needChange.xlsm")

    ' - OR -
    Workbooks("needChange.xlsm").Activate
    Set wbTarget = ActiveWorkbook


   'select cell A1 on the target book
   'wbTarget.Range("A1").Select

   'clear existing values form target book
   'wbTarget.Range("A1:M51").ClearContents

   'activate the source book
   wbThis.Activate

   'clear any thing on clipboard to maximize available memory
   Application.CutCopyMode = False

   'copy the range from source book
   wbThis.Sheets("Sprint backlog").Range("B6:B15").Copy

   'paste the data on the target book
   wbTarget.Sheets("Sheet1").Range("A14").PasteSpecial

   'clear any thing on clipboard to maximize available memory
   Application.CutCopyMode = False

   'save the target book
   'wbTarget.Save

   'close the workbook
   'wbTarget.Close

   'activate the source book again
   wbThis.Activate

   'clear memory
   Set wbTarget = Nothing
   Set wbThis = Nothing

End Sub

如果您只复制值(没有公式、图片、格式),一个简单的

Workbooks(Target).Sheets("Sheet1").Range("A14:A23").value = Workbooks(Source).Sheets("Sprint backlog").Range("B6:B15").value很好

(在同一个代码行中,只有 window 的大小让它看起来像 2)。

超过以下值:

Workbooks(Source).Sheets("Sprint backlog").Range("B6:B15").Copy _ Workbooks(Target).Sheets("Sheet1").Range("A14:A23").

(2 行)

注意_ 意味着下一行应该在同一行,只是为了更容易阅读代码。 (你在第二个代码中犯了这个错误)

注2range().paste不存在,只有sheets().paste,或者range().pastespecial.

注意 3 : 当然,所有的工作簿和工作表都必须存在并且与使用的名称完全相同...

注 4:copy/paste 仅在两个工作簿都已打开时才有效。对于已关闭的文件,情况就不同了。

简而言之,您犯了 2 个错误:_range().paste