我无法跨工作簿复制范围。有与此相关的类似帖子(VBA copy-paste offset to another workbook)但似乎没有任何帮助应用程序定义或对象定义的错误。
我试过这个
stdin
这也是:
Set wbSource = Workbooks("Source.xlsx")
Set wbTarget = Workbooks("Target.xlsx")
Set wbSource_WS = wbSource.Worksheets("Source")
Set wbSTarget_WS = wbTarget.Worksheets("Target")
wbSource_WS.Activate
wbSource_WS.Range(Cells(Row_SourceStart, Col_Source), Cells(Row_SourceEnd, Col_Source)).Copy
wbTarget_WS.Activate
wbSTarget_WS.Range(Cells(Row_TargetStart, Col_TargetStart), Cells(Row_TargetStart, Col_TargetEnd)).PasteSpecial Paste:=xlPasteValues
答案 0 :(得分:1)
您需要在范围中完全声明对象:
wbSource_WS.Activate
wbSource_WS.Range(Cells(Row_SourceStart, Col_Source), Cells(Row_SourceEnd, Col_Source)).Copy
应该是这样的:
wbSource_WS.Activate
wbSource_WS.Range(wbSource_WS.Cells(Row_SourceStart, Col_Source), wbSource_WS.Cells(Row_SourceEnd, Col_Source)).Copy
.range中的每个.cells都需要绑定到:wbSource_WS
使用With Statement会更简单/更清洁:
With wbSource_WS
.Range( .Cells(Row_SourceStart, Col_Source), .Cells(Row_SourceEnd, Col_Source)).Copy
End With
答案 1 :(得分:0)
您的代码中存在拼写错误。
Set wbSource = Workbooks("Source.xlsx")
Set wbTarget = Workbooks("Target.xlsx")
Set wbSource_WS = wbSource.Worksheets("Source")
Set wbSTarget_WS = wbTarget.Worksheets("Target")
wbSource_WS.Activate
wbSource_WS.Range(Cells(Row_SourceStart, Col_Source), Cells(Row_SourceEnd, Col_Source)).Copy
**wbSTarget_WS.Activate**
wbSTarget_WS.Range(Cells(Row_TargetStart, Col_TargetStart), Cells(Row_TargetStart, Col_TargetEnd)).PasteSpecial Paste:=xlPasteValues
我通过这个小改动运行它并没有产生任何错误。
我建议您使用Option Explicit。它可以防止再次发生此错误。 Excel会指出即使在你运行VBA程序之前还有一个未声明的变量。