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
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi Sushi,

This macro is applied to the workbook close event, so the records on or before today from column H will be cleared then copied over when you close your workbook.
  1. Navigate to Developer tab/Visual Basic/right click this Workbook in the left project view/View code
  2. Change the Top Left drop down to Workbook and the Top right drop down to Before Close
  3. Enter the following vba
  4. Add the Thaw Date criteria in I1 & I2 like in my sample

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
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("Sheet1").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
ABCDEFGHI
1Cut dateItemSalerepThaw WeightChildChild WeightThaw DateThaw Date
221/01/2022Halibut 40/u pAdam M (388)3521/01/2022<=44582
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
Cell Formulas
RangeFormula
I2I2="<="&TODAY()


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
319/01/2022Halibut 40/u pAdam M (388)3519/01/2022
423/01/2022Halibut 40/u pEllen Jones (323)30017/01/2022
524/01/2022Halibut U/20Adam M (388)2015/01/2022
624/01/2022Halibut 40/u pAdam M (388)7013/01/2022
724/01/2022Sablefish 7/u pAdam M (388)1011/01/2022
822/01/2022Halibut U/20Mike M (321)4509/01/2022
925/01/2022Halibut 40/u pEllen Jones (323)3007/01/2022
1024/01/2022C/R SockeyeEllen Jones (323)505/01/2022
1127/01/2022Halibut 40/u pEllen Jones (323)3003/01/2022
Sheet2
 
Upvote 0
Thanks so much. This is a shared workbook across multiple departments so the act of closing the file would actually work against the intended expectations. Any chance we can get it simply to where I can assign it to a button?
 
Upvote 0
Thats ok, insert a suitable shape right click on the new shape and click assign macro. Paste the code from above.
 
Upvote 0
Will it still auto-archive when the file closes?
You could have both running, if preferred. The macro can be run from a shape as a button or an event

I prefer to use the workbook close event just in case someone forgets to press the button.

This is what the code should look like when applied to a shape;

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("Sheet1").Range("I1").CurrentRegion
Set rgOutput = ThisWorkbook.Worksheets("Sheet2").Range("A1").CurrentRegion

rgdata.AdvancedFilter xlFilterCopy, rgcriteria, rgOutput

End Sub
 
Upvote 0
You could have both running, if preferred. The macro can be run from a shape as a button or an event

I prefer to use the workbook close event just in case someone forgets to press the button.

This is what the code should look like when applied to a shape;

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("Sheet1").Range("I1").CurrentRegion
Set rgOutput = ThisWorkbook.Worksheets("Sheet2").Range("A1").CurrentRegion

rgdata.AdvancedFilter xlFilterCopy, rgcriteria, rgOutput

End Sub
Getting an error

1643139297730.png


1643139348808.png


I have that formula in cell I1 (should it be in I2?). I only changed the names of the sheet. Am I missing something?
 
Upvote 0
Getting an error

View attachment 56089

View attachment 56090

I have that formula in cell I1 (should it be in I2?). I only changed the names of the sheet. Am I missing something?
Yes you need to have Thaw date in I1 and the <=TODAY() formula in I2 exactly like in my sample above.
Then excel knows the advanced filter criteria is for the Thaw dates column.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,215,523
Messages
6,125,315
Members
449,218
Latest member
Excel Master

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