【发布时间】:2018-07-25 00:39:36
【问题描述】:
我有以下代码,它是由以前的同事编写的,我需要帮助修改它。
加载用户表单,用户输入开始/结束日期。它在工作表 1 中搜索此开始/结束范围内的日期,然后将该整行复制到工作表 2,并继续向下工作表 1 搜索匹配的日期。
我需要修改为
- 在
Sheet1、Q列和S中搜索日期 - 在同一行中复制
Sheet1单元格C、G、J和日期Q和S - 粘贴到
Sheet2上A、B、C、D和E列中的一行。
这超出了我的知识水平。任何帮助将不胜感激,因为我似乎无法弄清楚这段代码。如果你能用简单的语言解释它是如何工作的,那同样很棒!
Dim rng As Range, destRow As Long
Dim shtSrc As Worksheet, shtDest As Worksheet
Dim c As Range '-- this is used to store the single cell in the For Each loop
Set shtSrc = Sheets("Sheet1") ' Sets "Sheet1" sheet as source sheet
Set shtDest = Sheets("Sheet2") 'Sets "Sheet2." sheet as destination sheet
destRow = 5 'Start copying to this row on destination sheet
' >> Set range to search for dates in Look Ahead period <<
Set rng = Application.Intersect(shtSrc.Range("P:P"), shtSrc.UsedRange)
' >> Look for matching dates in columns C to D <<
For Each c In rng.Cells
If (c.value >= startDate And c.value <= endDate) Or _
(c.Offset(0, 1).value >= startDate And c.Offset(0, 1).value <= endDate) Then ' Does date fall between start and end dates? If Yes, then copy to destination sheet
c.Offset(0, -2).Resize(1, 12).Copy _
shtDest.Cells(destRow, 1) 'Copy a 12 cell wide block to the other sheet, paste into Column A on row destRow
destRow = destRow + 1
' > Ends search for dates <
End If
Next
【问题讨论】: