Chels
Chels

Reputation: 29

How to copy certain row details into new workssheet if multiple condition is met, using VBA?

I'm trying to copy a row of data from one sheet to another if column "I" contains a certain letter (or phrase) and if "D" column (dates) is between, january, february and so on.

For instance, the data I'd like to copy is on a sheet called "Clients" and if column I contains the letter "T" then I'd like that data to auto-import into the next sheet called "Tax - Pending" with all those clients in the month of January.

PLEASE NOTE I ONLY WANT TO COPY THE ROW's CERTAIN DETAILS.

I have found VBA answer as but it only works for one criteria.

Sub CopyRow()
    Application.ScreenUpdating = False
    Dim x As Long, MaxRowList As Long, S As String, wsSource As Worksheet, wsTarget As Worksheet

    Set wsSource = ThisWorkbook.WorkSheets("Clients")
    Set wsTarget = ThisWorkbook.WorkSheets("Tax - Pending")
    iCol = 1
    MaxRowList = wsSource.Cells(Rows.Count, iCol).End(xlUp).Row
    For x = 3 To MaxRowList
        If InStr(1, wsSource.Cells(x, 9), "T") Then
            wsTarget.Rows(x).Value = wsSource.Rows(x).Value
        End If
    Next
    Application.ScreenUpdating = True
End Sub

Upvotes: 0

Views: 149

Answers (1)

A.S.H
A.S.H

Reputation: 29352

To check if the date at column D is within the current month, change your test

If InStr(1, wsSource.Cells(x, 9), "T") Then

into this:

If InStr(1, wsSource.Cells(x, 9), "T") And Month(wsSource.Cells(x, 4)) = Month(Now) Then

Upvotes: 1

Related Questions