2014-12-04 155 views
0

任何人都可以幫助解釋爲什麼我的範圍不完全更新,我可以如何強制? 單元格範圍更新但焦點仍保留在同一工作表上。然後,當它到達必須複製的下一行時失敗。Excel VBA複製表格不同的工作簿格式

 Workbooks.Open Filename:="U:\T\A.xlsx", UpdateLinks:=xlUpdateLinksNever 

    dblLastRow = 1 + Workbooks("A.xlsx").Worksheets("Z").Cells(Rows.Count, 1).End(xlUp).Row 

    Set rngReport2 = Workbooks("A.xlsx").Worksheets("Report2").Range(Cells(1, 1), Cells(dblLastRow, 7)) 
    rngReport2.Copy 
    Workbooks("B.xls").Worksheets("Y").Range("A1").PasteSpecial (xlPasteValues) 


    dblLastRow = Workbooks("A.xlsx").Worksheets("Sector code").Cells(Rows.Count, 1).End(xlUp).Row 

    Set rngReport2 = Workbooks("A.xlsx").Worksheets("Sector code").Range(Cells(1, 1), Cells(dblLastRow, 3)) 
    rngReport2.Copy 
    Workbooks("B.xls").Worksheets("X").Range("A1").PasteSpecial (xlPasteValues) 

回答

1

你應該總是以一個工作表的參考資格的Cells()

Sub test() 

    Dim wbA As Workbook, wbB As Workbook 
    Dim dblLastRow As Long 

    Set wbA = Workbooks.Open(Filename:="U:\T\A.xlsx", UpdateLinks:=xlUpdateLinksNever) 
    Set wbB = Workbooks("B.xls") 

    With wbA.Worksheets("Report2") 
     dblLastRow = 1 + .Cells(Rows.Count, 1).End(xlUp).Row 
     CopyValues .Range(.Cells(1, 1), .Cells(dblLastRow, 7)), wbB.Worksheets("Y").Range("A1") 
    End With 

    With wbA.Worksheets("Sector code") 
     dblLastRow = .Cells(Rows.Count, 1).End(xlUp).Row 
     CopyValues .Range(.Cells(1, 1), .Cells(dblLastRow, 3)), wbB.Worksheets("X").Range("A1") 
    End With 

End Sub 

Sub CopyValues(rngSrc As Range, rngDest As Range) 
    rngDest.Cells(1).Resize(rngSrc.Rows.Count, _ 
      rngSrc.Columns.Count).Value = rngSrc.Value 
End Sub 
+0

感謝蒂姆,我不是功能的風扇,但因爲我不得不重申的動作與其他範圍多做幾次,它確實有道理:) – ProtoVB 2014-12-05 16:53:07

相關問題