Copy/paste rows based on today's date

SushiJuice

New Member
Joined
Jan 20, 2022
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hello,

Long time fan of the YouTube channel, but first time posting here. I've implemented a lot of ideas from info gained from the shows, but I'm really green when it comes to VBA.

I'm trying to copy rows from Sheet 1 to Sheet 2 as an archive system so Sheet 2 would continually add more data as time goes on; essentially copying any rows with dates on or before today and pasting them into Sheet 2

The date column is H:H and pasting entire row to Sheet 2.

Please help

1642691672396.png
 
It looks like the top row with Optional is causing the error, when I insert a blank row A2:G2 so there is a gap between Optional and your table the macro works for me.
Sorry, still having issues.

I inserted a blank row at A2:G2 and I still get the error.

I completely deleted the the row with the "Optional" in it, and the blank row below it so all the column headers are in row 1, and I still get the error.

Is there any way you can attach the file you're using so I can take a look at it?
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Unfortunately I think there are rules against uploading sheets to the site hence why we use XL2BB to paste

Ok I've now placed the Thaw Date and Thaw date formula onto Sheet2 where your archive data is being saved like the following, note that the macro has also changed slightly;

VBA Code:
Sub RectangleRoundedCorners1_Click()

Dim rg As Range
Set rg = ThisWorkbook.Worksheets("Sheet2").Range("A1").CurrentRegion

rg.Offset(1).ClearContents

Dim rgdata As Range, rgcriteria As Range, rgOutput As Range

Set rgdata = ThisWorkbook.Worksheets("Sheet1").Range("A4").CurrentRegion
    Set rgcriteria = ThisWorkbook.Worksheets("Sheet2").Range("I1").CurrentRegion
Set rgOutput = ThisWorkbook.Worksheets("Sheet2").Range("A1").CurrentRegion

rgdata.AdvancedFilter xlFilterCopy, rgcriteria, rgOutput

End Sub

Copy - paste rows based on today's date SushiJuice.xlsm
ABCDEFG
1Cut dateItemSalerepThaw WeightChildChild WeightThaw Date
221/01/2022Halibut 40/u pAdam M (388)3521/01/2022
323/01/2022Halibut 40/u pEllen Jones (323)30023/01/2022
424/01/2022Halibut U/20Adam M (388)2022/01/2022
524/01/2022Halibut 40/u pAdam M (388)7024/01/2022
624/01/2022Sablefish 7/u pAdam M (388)1023/01/2022
722/01/2022Halibut U/20Mike M (321)45022/01/2022
825/01/2022Halibut 40/u pEllen Jones (323)30025/01/2022
924/01/2022C/R SockeyeEllen Jones (323)5024/01/2022
1027/01/2022Halibut 40/u pEllen Jones (323)30027/01/2022
1119/01/2022Halibut 40/u pAdam M (388)3519/01/2022
1223/01/2022Halibut 40/u pEllen Jones (323)30017/01/2022
1324/01/2022Halibut U/20Adam M (388)2015/01/2022
1424/01/2022Halibut 40/u pAdam M (388)7013/01/2022
1524/01/2022Sablefish 7/u pAdam M (388)1011/01/2022
1622/01/2022Halibut U/20Mike M (321)4509/01/2022
1725/01/2022Halibut 40/u pEllen Jones (323)3007/01/2022
1824/01/2022C/R SockeyeEllen Jones (323)505/01/2022
1927/01/2022Halibut 40/u pEllen Jones (323)3003/01/2022
Sheet1


Copy - paste rows based on today's date SushiJuice.xlsm
ABCDEFGHI
1Cut dateItemSalerepThaw WeightChildChild WeightThaw DateThaw Date
221/01/2022Halibut 40/u pAdam M (388)3521/01/2022<=44589
323/01/2022Halibut 40/u pEllen Jones (323)30023/01/2022
424/01/2022Halibut U/20Adam M (388)2022/01/2022
524/01/2022Halibut 40/u pAdam M (388)7024/01/2022
624/01/2022Sablefish 7/u pAdam M (388)1023/01/2022
722/01/2022Halibut U/20Mike M (321)45022/01/2022
825/01/2022Halibut 40/u pEllen Jones (323)30025/01/2022
924/01/2022C/R SockeyeEllen Jones (323)5024/01/2022
1027/01/2022Halibut 40/u pEllen Jones (323)30027/01/2022
1119/01/2022Halibut 40/u pAdam M (388)3519/01/2022
1223/01/2022Halibut 40/u pEllen Jones (323)30017/01/2022
1324/01/2022Halibut U/20Adam M (388)2015/01/2022
1424/01/2022Halibut 40/u pAdam M (388)7013/01/2022
1524/01/2022Sablefish 7/u pAdam M (388)1011/01/2022
1622/01/2022Halibut U/20Mike M (321)4509/01/2022
1725/01/2022Halibut 40/u pEllen Jones (323)3007/01/2022
1824/01/2022C/R SockeyeEllen Jones (323)505/01/2022
1927/01/2022Halibut 40/u pEllen Jones (323)3003/01/2022
Sheet2
Cell Formulas
RangeFormula
I2I2="<="&TODAY()
 
Upvote 0
Ok I no longer get the error! Great!!

But it partially works - the data gets transferred over to the archive, but the rows still remain on the original sheet. Is there a way to delete the rows once they're transferred?

thanks so much for your help so far
 
Upvote 0
Ok I no longer get the error! Great!!

But it partially works - the data gets transferred over to the archive, but the rows still remain on the original sheet. Is there a way to delete the rows once they're transferred?

thanks so much for your help so far

Updated code to delete data from Sheet1 after transfer, note that dates after todays date remain in Sheet1.

VBA Code:
Dim rg As Range
Set rg = ThisWorkbook.Worksheets("Sheet2").Range("A1").CurrentRegion

Application.ScreenUpdating = False
' Delete existing archive
    rg.Offset(1).ClearContents

Dim rgdata As Range, rgcriteria As Range, rgOutput As Range

Set rgdata = ThisWorkbook.Worksheets("Sheet1").Range("A4").CurrentRegion
    Set rgcriteria = ThisWorkbook.Worksheets("Sheet2").Range("I1").CurrentRegion
Set rgOutput = ThisWorkbook.Worksheets("Sheet2").Range("A1").CurrentRegion

' Filter & Copy to Sheet2
    rgdata.AdvancedFilter xlFilterCopy, rgcriteria, rgOutput

' Filter and Delete
rgdata.AdvancedFilter xlFilterInPlace, rgcriteria
    rgdata.Offset(1).ClearContents
    ThisWorkbook.Worksheets("Sheet1").ShowAllData
Application.ScreenUpdating = True
 
Upvote 0
Hello SushiJuice,

Just automating the AutoFilter should work for you:-

VBA Code:
Sub Test()

Application.ScreenUpdating = False

        With Sheet1.[A1].CurrentRegion
                .AutoFilter 7, "<=" & [Today()]
                .Offset(1).EntireRow.Copy Sheet2.Range("A" & Rows.Count).End(3)(2)
                .Offset(1).EntireRow.Delete
                .AutoFilter
        End With
        
Sheet2.Columns.AutoFit
Application.ScreenUpdating = True

End Sub

I'm assuming that your data does actually start in Column A with headings in row1 which means that the thaw dates are in Column G.
I've use the sheet codes in the macro, not the sheet names.
Assign the code to a button.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Updated code to delete data from Sheet1 after transfer, note that dates after todays date remain in Sheet1.

VBA Code:
Dim rg As Range
Set rg = ThisWorkbook.Worksheets("Sheet2").Range("A1").CurrentRegion

Application.ScreenUpdating = False
' Delete existing archive
    rg.Offset(1).ClearContents

Dim rgdata As Range, rgcriteria As Range, rgOutput As Range

Set rgdata = ThisWorkbook.Worksheets("Sheet1").Range("A4").CurrentRegion
    Set rgcriteria = ThisWorkbook.Worksheets("Sheet2").Range("I1").CurrentRegion
Set rgOutput = ThisWorkbook.Worksheets("Sheet2").Range("A1").CurrentRegion

' Filter & Copy to Sheet2
    rgdata.AdvancedFilter xlFilterCopy, rgcriteria, rgOutput

' Filter and Delete
rgdata.AdvancedFilter xlFilterInPlace, rgcriteria
    rgdata.Offset(1).ClearContents
    ThisWorkbook.Worksheets("Sheet1").ShowAllData
Application.ScreenUpdating = True
The new macro just deleted all of my data.

At this point I'm giving up.

Thanks for your help
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,755
Members
449,094
Latest member
dsharae57

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