Excel VBA:将范围复制到新工作簿

时间:2013-05-06 19:48:30

标签: vba excel-vba range copy-paste excel

我有以下代码,工作一小时后,现在不工作突然:-( 我在这里错过了什么吗?

 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")

3 个答案:

答案 0 :(得分:1)

尝试以下

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

答案 1 :(得分:0)

你错过了第二行的第一句话:

Set ReportWorkbook = Workbooks.Open("P:\Env_Check_Report\Results\" & "Report.xlsx", UpdateLinks:=True)

答案 2 :(得分:0)

你有没有试过像:

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

您还可以使用以下代码在代码中创建新工作簿:

Set wbDes = Workbooks.Add(xlWBATWorksheet)