将工作簿的内容复制到另一个工作簿

时间:2018-09-10 13:50:35

标签: excel vba

我想将一个工作簿的工作表的内容复制到另一个工作簿。

此代码仅打开两个工作簿,而不复制和粘贴值。

Sub btnClickMev2()
Dim x As Workbook
Dim y As Workbook

'Open Workbooks
Set x = Workbooks.Open("H:\Desktop\test.xls")
Set y = Workbooks.Open("H:\Desktop\Book1v3.xlsm")

'Copy from x Workbook
x.Sheets("Query2").Range("A1:T1036").Copy
'Paste to Y Workbook
y.Sheets("Sheet2").Range("A1:T1036").PasteSpecial
'Close x:
x.Close
End Sub

1 个答案:

答案 0 :(得分:3)

希望这会有所帮助,可以在Thread上找到它并为您的代码重写:

Sub foo3()
Dim x As Workbook
Dim y As Workbook
Dim vals as Variant

'## Open both workbooks first:
Set x = Workbooks.Open("H:\Desktop\test.xls")
Set y = Workbooks.Open("H:\Desktop\Book1v3.xlsm")

'Store the value in a variable:
vals = x.Sheets("Query2").Range("A1:T1036").Value

'Use the variable to assign a value to the other file/sheet:
y.Sheets("Sheet2").Range("A1:T1036").Value = vals 

'Close x:
x.Close

End Sub