VBA code to move entire rows once date has passed

cornflower13

New Member
Joined
Nov 17, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I have tried so many other suggestions from various internet sources but nothing seems to do what I need it to. I'm a fairly basic user so please be kind :)

I want my macro to move (not just copy) entire rows from a table on sheet 1 "DIARY" to a table on sheet 2 "HISTORICAL" once the date has passed. Today's booking should stay on sheet 1 until the day is over. The date is in column A.

I'd also like this to happen automatically if possible. So for example, every time the sheet is opened, everything from before today (but not including today) moves from the diary tab to the historical tab.

Please help :(
 

Attachments

  • sheet1.JPG
    sheet1.JPG
    213.1 KB · Views: 23
  • sheet2.JPG
    sheet2.JPG
    158.7 KB · Views: 23

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Can you post in XL2BB, or File Share your Workbook?
 
Upvote 0
Please try the following on a copy of your workbook. Right click on the DIARY tab, select View Code, and place the code in the window that appears on the right of screen.
VBA Code:
Private Sub Worksheet_Activate()
    Dim d As Date
    d = Format(Date - 1, "dd mmmm yyyy")
    Dim Lob1 As ListObject
    Set Lob1 = Me.ListObjects(1)
    Dim ws As Worksheet
    Set ws = Worksheets("HISTORICAL")
    Lob1.AutoFilter.ShowAllData
    With Lob1.Range
        .AutoFilter 1, "<=" & CLng(d)
        If WorksheetFunction.Subtotal(3, .Columns(1)) > 2 Then
            .Offset(1).Resize(.Rows.Count - 1).Copy ws.Cells(Rows.Count, 1).End(xlUp)
            .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
        End If
    End With
    Lob1.AutoFilter.ShowAllData
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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