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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43490572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:29:13  来源:igfitidea点击:

VBA copy data from other workbook without open that workbook

excelvbaexcel-vba

提问by Adryan Permana

I have this VBA code:

我有这个 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

I want to copy data from "part8.xlsx" without open that file. How can I do that? Many thanks for the help.

我想在不打开该文件的情况下从“part8.xlsx”复制数据。我怎样才能做到这一点?非常感谢您的帮助。

回答by MaddyN

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

Try This

尝试这个