总的来说,今天我刚开始学习VBA。我基本上只是遵循了这个家伙的指示,该指示已经产生了奇迹:www.excelcampus.com/vba/copy-paste-another-workbook/。但是,现在我想反过来做。因此,这意味着从Sheet1复制“ A2:IG2”并将其粘贴到“ A2:IG2” Sheet2,然后复制“ A3:IG3”并将其粘贴到“ A2:IG2” Sheet2。在从Sheet1复制所有值之前,我想一直保持循环。
Sub Copy_Paste_Below_Last_Cell()
'Find the last used row in both sheets and copy and paste data below existing data.
'Open method requires full file path to be referenced.
Workbooks.Open "C:\Users\xxxx\OneDrive\Desktop\Sheet2.xlsx"
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("Sheet2.xlsx").Worksheets("Sheet2")
Set wsDest = Workbooks("Sheet1.xlsx").Worksheets("Sheet1")
'1. Find last used row in the copy range based on data in column A
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Offset(1).Row
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Row
'3. Copy & Paste Data
wsCopy.Range("A2:IG2" & lCopyLastRow).Copy _
wsDest.Range("A" & lDestLastRow)
Workbooks("Sheet2").Close SaveChanges:=True
End Sub
上面的代码是我认为可以使用的修改版本,但不是专门复制“ A2:IG2”,而是复制了范围A:IG和偏移行中的所有可用日期。
答案 0 :(得分:0)
只需更改最后一部分
'3. Copy & Paste Data
wsCopy.Range("A2:IG2").Copy _
wsDest.Range("A" & lDestLastRow + 1)
Workbooks("Sheet2").Close SaveChanges:=True