簡體   English   中英

Excel VBA:如何根據具有附加條件的數據范圍將行從一個工作表復制到另一個工作表以縮小結果?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM