hMur
hMur

Reputation: 3

Excel VBA: How to copy row from one worksheet to another based on data range with additional criteria to narrow down results?

I have the below code that copies rows from one worksheet to another based on a date range in column B. However, struggling to add a second condition that would eliminate results based on another column (C) containing text "Action". Can anyone help on this?

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

Upvotes: 0

Views: 51

Answers (1)

Samuel Hulla
Samuel Hulla

Reputation: 7089

Use offset to compare the data in column C.
Add the following condition inside your If statement, appended with 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 

Upvotes: 1

Related Questions