2017-09-20 84 views
0

我正在從一個工作簿中複製一組單個列並將它們粘貼到另一個列上。我想抵消我的數據被複制到哪一行(例如向下9行)。我試圖使用屬性:使用VBA偏移各個複製列

.Offset() 

但我不知道如何將它添加到我現有的代碼。

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

Set sourceProject = Workbooks("Data to Copy.xlsm").Worksheets(2).Columns("C") 
Set targetProject = Workbooks("Data 
Destination.xlsm").Worksheets(1).Columns("D") 

Set sourcePM = Workbooks("Data to Copy.xlsm").Worksheets(2).Columns("D") 
Set targetPM = Workbooks("Data Destination.xlsm").Worksheets(1).Columns("I") 

Set sourceBusiness = Workbooks("Data to Copy.xlsm").Worksheets(2).Columns("E") 
Set targetBusiness = Workbooks("Data Destination.xlsm").Worksheets(1).Columns("K") 

Set sourceHigh = Workbooks("Data to Copy.xlsm").Worksheets(2).Columns("F") 
Set targetHigh = Workbooks("Data Destination.xlsm").Worksheets(1).Columns("L") 

Set sourceE0 = Workbooks("Data to Copy.xlsm").Worksheets(2).Columns("G") 
Set targetE0 = Workbooks("Data Destination.xlsm").Worksheets(1).Columns("S") 

Set sourceActual = Workbooks("Data to Copy.xlsm").Worksheets(2).Columns("H") 
Set targetActual = Workbooks("Data Destination.xlsm").Worksheets(1).Columns("V") 

sourceTitle.Copy Destination:=targetTitle 
sourceProject.Copy Destination:=targetProject 
sourcePM.Copy Destination:=targetPM 
sourceBusiness.Copy Destination:=targetBusiness 
sourceHigh.Copy Destination:=targetHigh 
sourceE0.Copy Destination:=targetE0 
sourceActual.Copy Destination:=targetActual 
End Sub 
+1

您無法偏移全列參考的行。你將需要找到數據的最後一行,限制你的範圍,然後你可以使用偏移量。 –

+0

Offset的格式通常是這樣的:'Range(「B1」).Offset(9,0)= Value'其中9是行而0是列。 – Mitch

回答

0

正如Scott和Mitch指出的那樣,您需要一個具有定義的開始和結束行/列的選定範圍。

可以具有類似於代碼一些動態代碼做到這一點(每個源片/列):

Dim LR as Long, col1 as String 
col1 = "C" 'Change to whatever column you want to focus 
With Sheets("NAME1") 
    LR = .Cells(.Rows.Count, col1).End(xlUp).Row 
End With 

然後,可以使用現在被定義LR移動至數據(LR =最後一行):

With Sheets("NAME1") 
    .Range(.Cells(1, col1), .Cells(LR, col1)).Copy Sheets("NAME2").Cells(9,"D") 
End With 

您只需在粘貼到目標範圍時指定左上角的單元格。