2017-09-19 80 views
0

我試圖將數據從一個工作簿複製到另一個工作簿。我已經可以做到這一點,但我無法將數據向下偏移一些行。我的代碼的最後部分是我的問題。使用VBA從現有數據向下偏移目標數據

Sub T1() 
Dim sourceTitle As Range, targetTitle As Range 

Set sourceTitle = Workbooks("Data to Copy.xlsm").Worksheets(2).Columns("B") 
Set targetTitle = Workbooks("Data 
Destination.xlsm").Worksheets(1).Columns("A") 

sourceTitle.Copy Destination:=targetTitle.Cells(Rows.Count, 1).End(xlUp).Offset(2, 0) 
End Sub 
+0

嘗試targetTitle.Cells(Rows.Count,1).END(xlUp).row.Offset(2,0)。以前從來沒有見過這樣做。我可以用這個:) – Hrothgar

+0

其他建議?我得到運行時錯誤'424':需要對象 – Chris

回答

2

的代碼,寫入,試圖將數據的整列複製成範圍小於一整列,這將超越在Excel行限制的數據。很少需要複製整列(如果是,則轉移到數據庫解決方案)。

嘗試這種情況:

Sub T1() 


    Dim wsSource as Worksheet, wsTitle as Worksheet 
    Set wsSource = Workbooks("Data to Copy.xlsm").Worksheets(2) 
    Set wsTarget = Workbooks("Data Destination.xlsm").Worksheets(1) 

    Dim sourceTitle as Range 
    Set sourceTitle = wsSource.Range(wsSource.Range("B1"),wsSource.Cells(Rows.Count,2).End(xlUp)) 

    sourceTitle.Copy Destination:=wsTarget.Cells(Rows.Count,1).End(xlUp).Offset(2) 

End Sub