VBA从其他工作簿复制数据而不打开该工作簿

时间:2017-04-19 08:38:01

标签: excel vba excel-vba

我有这个VBA代码:

Sub OpenCopyPaste()

Dim wb As Workbook
Dim Reportwb As Workbook

' set report workbook to workbook object (works only is the file is already open)
Set Reportwb = Workbooks("report.xlsx")

' open the source workbook and select the source sheet
Set wb = Workbooks.Open(Filename:="C:\Users\Adryan Permana\Downloads\Test\part8.xlsx")

' copy the source range and paste in 1 line , paste at "C3:E9"
wb.Sheets("Sheet1").Range("C3:E9").Copy
Reportwb.Sheets("Sheet1").Range("C3:E9").PasteSpecial xlPasteValues
Reportwb.Save

End Sub

我想复制来自" part8.xlsx"的数据。没有打开那个文件。我怎样才能做到这一点?非常感谢你的帮助。

1 个答案:

答案 0 :(得分:-1)

Sub OpenCopyPaste()
Dim wb As Workbook
Dim Reportwb As Workbook
' set report workbook to workbook object (works only is the file is already 
'open)
Set Reportwb = ThisWorkbook
Application.ScreenUpdating = False
' open the source workbook and select the source sheet
Set wb = Workbooks.Open(Filename:="C:\Users\Adryan Permana\Downloads\Test\part8.xlsx")
' copy the source range and paste in 1 line , paste at "C3:E9"
wb.Sheets("Sheet1").Range("C3:E9").Copy
Reportwb.Sheets("Sheet1").Range("C3:E9").PasteSpecial xlPasteValues
wb.Close False
Reportwb.Save
Application.ScreenUpdating = True
End Sub

试试这个