简体   繁体   English

在两个工作簿之间复制数据

[英]Copying data between two workbooks

I'm trying to copy data from a closed workbook to another closed workbook but I keep getting Runtime error 9, subscript out of range . 我正在尝试将数据从一个已关闭的工作簿复制到另一个已关闭的工作簿,但是我不断遇到Runtime error 9, subscript out of range Both files are placed on my desktop under the same path. 这两个文件都放在同一路径下的桌面上。 I am trying this example from a book I am learning from: 我正在从我从中学习的书中尝试以下示例:

Sub CopyRange3()
    Dim Rng1 As Range, Rng2 As Range
    Set Rng1 = Workbooks(“File1.xlsx”).Sheets(“Sheet1”).Range(“A1”)
    Set Rng2 = Workbooks(“File2.xlsx”).Sheets(“Sheet2”).Range(“A1”)
    Rng1.Copy Rng2
End Sub

Can someone explain what is going wrong with this code? 有人可以解释这段代码出了什么问题吗?

Workbooks is a collection of the currently open workbooks. 工作簿是当前打开的工作簿的集合。 To use this you'll need to open them both. 要使用此功能,您需要同时打开它们。

You can turn off screen-updating, open them both, copy the data, close them both, saving the second book. 您可以关闭屏幕更新,打开它们两个,复制数据,关闭它们两个,保存第二本书。


You get the error message "Subscript out of range" because "File1.xslx" is not a member of Workbooks, because it isn't open. 您收到错误消息“下标超出范围”,因为“ File1.xslx”不是工作簿的成员,因为它没有打开。

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

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