Excel VBA:运行时错误“ 1004”:Range类的PasteSpecial方法失败

时间:2019-11-15 18:22:54

标签: excel vba

我想将一个单元格的范围从一个关闭的工作簿复制到另一个工作簿。这是我的代码:

 Workbooks.Open Filename:="C:\Users\yixuanjiang\Desktop\Performance Report\Risk Test-simplified.xls"
 Workbooks("Risk Test-simplified").Worksheets("Summary").Activate
 Workbooks("Risk Test-simplified").Worksheets("Summary").Range("B9:F16").Copy
 Workbooks("Summary-v1.0").Activate
 Worksheets("Automation").Range("B155").PasteSpecial

 Workbooks("Risk Test-simplified").Worksheets("Summary").Activate
 Workbooks("Risk Test-simplified").Worksheets("Summary").Range("Q9:U22").Copy
 Workbooks("Summary-v1.0").Activate
 Worksheets("Automation").Range("H152").PasteSpecial

我遇到错误的那一行是最后一行: Worksheets(“ Automation”)。Range(“ H152”)。PasteSpecial

谁能告诉我为什么会这样?

1 个答案:

答案 0 :(得分:0)

假设您在使用了一定范围的单元格之后,可以执行以下操作。

我认为失败的原因是因为您没有达到范围要求。

Sub CopyData()

Dim MasterWS As Worksheet
Dim ws As Worksheet
Dim CopyRange As Variant
Dim lrowOtherSheets As Long
Dim LRow As Long

Set MasterWS = Sheets(1)
Set ws = ActiveSheet

Set CopyRange = ws.UsedRange
CopyRange.Copy

LRow = .Range("A" & .Rows.Count).End(xlUp).Row
MasterWS.Range("A" & LRow).PasteSpecial Paste:=xlPasteAll, Transpose:=False
Application.CutCopyMode = False

End Sub