我目前正在尝试将动态范围从一个工作簿复制并粘贴到另一个工作簿,以刷新现有的信息中心。
我已经开发了以下代码,但继续在下面的斜体部分收到错误通知:
Sub TransferData()
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("C:\file1.xlsx")
Set y = Workbooks.Open("C:\file2.xlsx")
Set StartCell = Range("A1")
finalrow = x.Sheets("Worksheet Name").Cells(Rows.Count, 2).End(xlUp).Row
'Counts
FinalColumn = x.Sheets("Worksheet Name").Cells(1,
Columns.Count).End(xlToLeft).Column
*x.Sheets("Worksheet Name").Range(StartCell, Cells(FinalRow,
FinalColumn)).Copy*
y.Sheets("Worksheet Name").Range(Cells(1, 7), Cells(FinalRow,
FinalColumn)).Paste
End Sub
答案 0 :(得分:0)
您必须声明工作表,它也是Cells的“父级”。像这样:
With x.Sheets("Worksheet Name")
.Range(StartCell, .Cells(finalrow, FinalColumn)).Copy
End With
With y.Sheets("Worksheet Name")
.Range(.Cells(1, 7)).PasteSpecial xlPasteAll
End With