【发布时间】:2019-07-08 21:46:41
【问题描述】:
我希望我的代码复制整个工作表(SOURCE)并将其粘贴到其他工作簿(WHERE_I_WANNA_PASTE_IT)下的其他工作表(TARGET)中并保存。
我收到此错误:
Run=-time error '1004': Range 类的复制方法失败
在这一行:
CurrentSheet.Cells.Copy Destination:=oSheet.cells
代码:
Public Const path1 As String = "C:\Where_I_WANNA_PASTE_IT.xlsb"
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim CurrentSheet As Object
Sub copyNpaste
Set CurrentSheet = ActiveSheet
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(path1)
Set oSheet = oBook.Worksheets("TARGET")
'Deleting what's on "TARGET" first
oSheet.cells.delete
'This is where the Error happens.
CurrentSheet.Cells.Copy _
Destination:=oSheet.Cells
oBook.Save
oBook.Close
Set oExcel = Nothing
Set oBook = Nothing
Set oSheet = Nothing
Set CurrentSheet = Nothing
End Sub
【问题讨论】:
-
我无法重现您的错误。您使用后期绑定是否有原因?
-
试试这个,看看它是否有效。
CurrentSheet.Cells.CopySpecial
标签: vba excel copy-paste worksheet