将范围复制到新工作簿

时间:2014-08-15 09:43:03

标签: vba

我正在尝试将范围从一个工作簿复制到另一个工作簿。但是我得到一个运行时错误,我的下标超出了范围。这是违规行:

Set wbDest = Workbooks("newFile.xlsx")

我错过了什么?

代码

Dim wbDest As Workbook
Dim ws2 As Worksheet

Set myRange = Range("A1", Cells(LastRow, LastCol).Address)' Values retrieved from function
Set wbDest = Workbooks("newFile.xlsx")'both newFile and Thisworkbook stored on my desktop
Set ws2 = wbDest.Sheets("Sheet 1")
ws2.Range("A1") = wsJournals.myRange.Value

1 个答案:

答案 0 :(得分:2)

当您打开工作簿时,您需要确保提供工作簿的完整路径并打开它。以下是语法

Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks.Open(Filename:="C:\Users\user1\Desktop\Book1.xlsx")
Set ws = wb.Sheets("sheet1")

然后您可以使用ws选择特定的单元格并执行其他操作。它对我有用。