Excel VBA:将范围复制到新工作簿
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16406105/
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-11 21:00:01 来源:igfitidea点击:
Excel VBA : Copy Range to a new workbook
提问by Jill448
I have the below code which was working an hour back and not working now suddenely :-( Am I missing anything here?
我有下面的代码,它在一个小时前工作,现在突然不工作了:-(我在这里遗漏了什么吗?
Set NewWorkbook = Workbooks.Open("P:\Env_Check_Report\Results\" & "ENV_Report_ORG.xlsx", UpdateLinks:=True)
Set ReportWorkbook = Workbooks.Open("P:\Env_Check_Report\Results\" & "Report.xlsx", UpdateLinks:=True)
ReportWorkbook.Sheet5.Range("B2:T10").Copy _
Destination:=NewWorkbook.Sheet2.Range("B2:T10")
回答by Santosh
Try below
试试下面
Dim xlObj As Object
Set xlObj = CreateObject("Excel.Application")
Set NewWorkbook = xlObj.Workbooks.Open("P:\Env_Check_Report\Results\" & "ENV_Report_ORG.xlsx", UpdateLinks:=True)
Set ReportWorkbook = xlObj.Workbooks.Open("P:\Env_Check_Report\Results\" & "Report.xlsx", UpdateLinks:=True)
Set NewWorksht = NewWorkbook.Sheets("Sheet5")
Set Reportsht = ReportWorkbook.Sheets("Sheet2")
If Not NewWorkbook Is Nothing And Not ReportWorkbook Is Nothing Then
NewWorksht.Range("B2:T10").Copy
Reportsht.Range("B2:T10").PasteSpecial
End If
回答by hrezs
Have you tried something like:
您是否尝试过类似的事情:
Set wbSrc = Workbooks.Open(Filename:=MyPath & "\" & strFilename)
wbSrc.Worksheets(5).Range("B2:T10").Copy
Set wbDes = Workbooks.Open(Filename:=MyPath2 & "\" & strFilename2)
wbDes.Worksheets(2).Range("B2:T10").Paste
You can also create a new workbook in your code with:
您还可以在代码中创建一个新的工作簿:
Set wbDes = Workbooks.Add(xlWBATWorksheet)
回答by Phylogenesis
You're missing the first quote on the second line:
你错过了第二行的第一个引号:
Set ReportWorkbook = Workbooks.Open("P:\Env_Check_Report\Results\" & "Report.xlsx", UpdateLinks:=True)