小编J L*_*Lee的帖子

将活动工作表复制到另一个工作簿:范围类的复制方法失败

我希望我的代码复制整个工作表(SOURCE)并将其粘贴到其他工作簿(WHERE_I_WANNA_PASTE_IT)下的其他工作表(TARGET)中并保存.

我收到此错误:

运行= -time错误'1004':范围类的复制方法失败

在这条线上:

CurrentSheet.Cells.Copy Destination:=oSheet.cells 
Run Code Online (Sandbox Code Playgroud)

代码:

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 …
Run Code Online (Sandbox Code Playgroud)

excel vba copy-paste worksheet excel-vba

1
推荐指数
1
解决办法
7465
查看次数

标签 统计

copy-paste ×1

excel ×1

excel-vba ×1

vba ×1

worksheet ×1