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

标签 excel excel-2013 vba

所以我有这个代码:

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

它给了我标题中表达的运行时错误“9”。代码非常简单,我完全被难住了。
我在网上阅读,似乎是因为名称不存在,但是工​​作表和工作簿都存在,名称相同。任何代码之间都没有空格或奇怪的字符。

基本上,我想将 Source.xlsm 中的“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),因为它找不到或无法访问它并因此返回问题,但我不知道如何用代码修复它。

如果有帮助,则在运行宏时,此代码中的两个工作簿对我来说都是打开且可访问的。

我正在向你寻求帮助。

非常感谢。
此致。

最佳答案

这比预期的要棘手。我从这个网页上借了很多东西 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

关于Excel 2013 VBA : Subscript out of Range (Error 9),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35701665/

相关文章:

excel - Office URI 方案忽略内容处置

vb.net - 使用字符串值创建 Excel

Java 程序从 Excel 文件读取数据抛出错误..需要帮助解决

vba - 将 excel 默认光标更改为箭头指针

excel - 将单元格范围从一个工作簿复制到另一个工作簿

vba - 如何在VBA中输入UTF-8

Python pandas dataframe 从列中选择行

excel - 为什么我的索引匹配函数返回 #N/A 错误?

excel - 从范围(对象)中删除单元格

excel - 如何后期绑定(bind)项目中的类模块?