作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望我的代码复制整个工作表(SOURCE)并将其粘贴到其他工作簿(WHERE_I_WANNA_PASTE_IT)下的其他工作表(TARGET)中并保存。
我收到此错误:
Run=-time error '1004': Copy Method of Range class failed
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
最佳答案
一些建议,这应该有效(至少它在我的电脑上有效,我能够完美地复制你的错误):
修复 1
不要创建新的 Excel 应用程序,使用相同的线程;换句话说,删除这个:
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(path1)
Set oBook = Workbooks.Open(path1)
Set CurrentSheet = ThisWorkbook.ActiveSheet
Public Const path1 As String = "C:\yourfile.xlsb"
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim CurrentSheet As Object
Sub copyNpaste()
Set oBook = Workbooks.Open(path1)
Set oSheet = oBook.Worksheets("TARGET")
Set CurrentSheet = ThisWorkbook.ActiveSheet
On Error GoTo ESCAPE 'if the code fails, we go to "Escape" and at least we close the file
'Deleting what's on "TARGET" first
oSheet.Cells.Delete
'This is where the Error happens.
CurrentSheet.Cells.Copy _
Destination:=oSheet.Cells
oBook.Save
ESCAPE:
oBook.Close
Set oExcel = Nothing
Set oBook = Nothing
Set oSheet = Nothing
Set CurrentSheet = Nothing
End Sub
关于vba - 将事件工作表复制到另一个工作簿 : Copy Method of Range class failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27928293/
我是一名优秀的程序员,十分优秀!