[英]Excel VBA: How to copy row from one worksheet to another based on data range with additional criteria to narrow down results?
我有以下代碼,該代碼根據B列中的日期范圍將行從一個工作表復制到另一個工作表。但是,努力添加第二個條件將消除基於包含文本“ Action”的另一列(C)的結果。 有人可以幫忙嗎?
Private Sub CommandButton3_Click()
Dim startdate As Date, enddate As Date
Dim rng As Range, destRow As Long
Dim shtSrc As Worksheet, shtDest As Worksheet
Dim c As Range
Set shtSrc = Sheets("Tier2")
Set shtDest = Sheets("Parameters")
destRow = 74
startdate = DateSerial(Year(Now), Month(Now), 1)
enddate = DateSerial(Year(Now), Month(Now) + 3, 0)
Set rng = Application.Intersect(shtSrc.Range("B:B"), shtSrc.UsedRange)
For Each c In rng.Cells
If c.Value >= startdate And c.Value <= enddate Then
c.Offset(0, 1).Resize(1, 10).Copy _
shtDest.Cells(destRow, 1)
destRow = destRow + 1
End If
Next
End Sub
使用偏移量比較列C中的數據。
在If
語句內添加以下條件,並附加And
For Each c In rng.Cells
If c.Value >= startdate And c.Value <= enddate And c.Offset(0, 1) <> "Action" Then
'....code....
End If
Next c
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.