【发布时间】:2019-04-24 23:58:04
【问题描述】:
您好,我是 VBA 的新手,我想循环这段代码。截至目前,此代码正在将范围(“B30:B”)中的数据从“Ba Pricing”复制到目标工作表,并将相同的数据复制到最后一个单元格。我想做的是循环这段代码,它将复制列(E、H、K 和 N)中的数据并将其粘贴到“Loader”工作表的最后一个单元格。如果每列都没有数据,则不复制。
谢谢
Sub Copy_Paste_Below_Last_Cell()
'Find the last used row in both sheets and copy and paste data below existing data.
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 = ThisWorkbook.Worksheets("Ba pricing")
Set wsDest = ThisWorkbook.Worksheets("Loader")
'1. Find last used row in the copy range based on data in column A
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).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, "C").End(xlUp).Offset(1).Row
'3. Copy & Paste Data
wsCopy.Range("B30:B" & lCopyLastRow).Copy
wsDest.Range("C" & lDestLastRow).PasteSpecial xlPasteValues
End Sub
它应该看起来像这样......
PriceDate
4/23/2019
4/24/2019
4/25/2019
4/26/2019
4/27/2019
4/28/2019
4/29/2019
4/30/2019
5/1/2019
5/2/2019
5/3/2019
5/4/2019
5/5/2019
5/6/2019
5/7/2019
5/8/2019
5/9/2019
5/10/2019
5/11/2019
5/12/2019
5/13/2019
5/14/2019
5/15/2019
5/16/2019
4/23/2019
4/24/2019
4/25/2019
4/26/2019
4/27/2019
4/28/2019
4/29/2019
4/30/2019
5/1/2019
5/2/2019
5/3/2019
5/4/2019
5/5/2019
5/6/2019
4/23/2019
【问题讨论】: