Copy If Date less than Today

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Hello all

Trying to copy all rows to another sheet if column 8 value is not today's date.

this is the loop I am using.

Thank you

Sub copyif()

Dim Lr As Long, lr2 As Long

Lr = Sheets("Report").Cells(Rows.Count, 1).End(xlUp).Row
lr2 = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To Lr

If Sheets("Report").Cells(i, 8).Value < Date Then
Rows(i).Copy Destination:=Sheets("Sheet1").Range("A").End(xlUp).Row
lr2 = Sheets("sheet1").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("Report").Rows(1).Copy Destination:=Sheets("Sheet1").Rows(1)

End If

Next i


End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Although there are faster ways, just to correct your syntax

Code:
Rows(i).Copy Destination:=[COLOR="#FF0000"]Sheets("Sheet1").Range("A").End(xlUp).Row[/COLOR]

could be written as
Code:
Rows(i).Copy Destination:=Sheets("Sheet1").Range("A") [COLOR="#FF0000"]& Rows.count[/COLOR]).End(xlUp).[COLOR="#FF0000"]Offset(1)[/COLOR]

or

Code:
 Sheets("Report").Rows(i).Copy Destination:=Sheets("Sheet1").[COLOR="#FF0000"]Rows[/COLOR](Sheets("Sheet1").Range("A" & Rows.Count).[COLOR="#FF0000"]End(xlUp).Row[/COLOR]).[COLOR="#FF0000"]Offset(1)[/COLOR]

i.e.

Code:
Sub copyif()

    Dim Lr As Long, i As Long

    Lr = Sheets("Report").Cells(Rows.Count, 1).End(xlUp).Row

    For i = 2 To Lr

        If Sheets("Report").Cells(i, 8).Value < Date Then
            Sheets("Report").Rows(i).Copy Destination:=Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1)

            Sheets("Report").Rows(1).Copy Destination:=Sheets("Sheet1").Rows(1)

        End If

    Next i


End Sub

or
Code:
Sub copyif()

    Dim Lr As Long, i As Long

    Lr = Sheets("Report").Cells(Rows.Count, 1).End(xlUp).Row

    For i = 2 To Lr

        If Sheets("Report").Cells(i, 8).Value < Date Then
            Sheets("Report").Rows(i).Copy Destination:=Sheets("Sheet1").Rows(Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row).Offset(1)

            Sheets("Report").Rows(1).Copy Destination:=Sheets("Sheet1").Rows(1)

        End If

    Next i


End Sub

Btw the codes above are for less than today's date as per your code, if you want different to today's date as per the question then change < to <> in both codes.
 
Last edited:
Upvote 0
Thank you for the code both work perfectly. Most important thank you for the syntax correction, great help.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top