Copy rows to a new workbook based on a date range of last three months from today()

dandee14k

New Member
Joined
Dec 1, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hello me again,

I have the below code, which works perfectly to copy rows from one sheet to another based on the value of another cell.

I'm trying now to run the same sort of thing but I need to move the rows from one Workbook (main source.xlsb) to another Workbook (Archive 2023.xlsb) but I needed to be based on a date range (so basically to retain the last three months of data and move everything else to the archive my date source should be in column AB2:AB )

Just being conscious that once the main source gets to big it will slowdown the sheet.

Please let me know if this is possible and thanks again in advance.

VBA Code:
Sub MoveCellsSEA()

Dim xRg As Range

Dim xCell As Range

Dim A As Long

Dim B As Long

Dim C As Long

A = Worksheets("Files to Make Up (Sea) ").UsedRange.Rows.Count
B = Worksheets("Archive (Sea)").UsedRange.Rows.Count
If B = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("Archive (Sea)").UsedRange) = 0 Then B = 0
End If
Set xRg = Worksheets("Files to Make Up (Sea) ").Range("A2:AG" & A)
On Error Resume Next
Application.ScreenUpdating = False
For C = 1 To xRg.Count
If CStr(xRg(C).Value) = "Completed" Then
xRg(C).EntireRow.COPY
Worksheets("Archive (Sea)").Range("A" & B + 1).PasteSpecial Paste:=xlPasteValues
xRg(C).EntireRow.Delete
If CStr(xRg(C).Value) <> "" = "Completed" Then
C = C - 1
End If
B = B + 1
End If
Next
Application.ScreenUpdating = True

End Sub

Source Woorkbook.

Main Source.xlsb
NOPQRSTUVWXYZAAABACADAEAFAG
1File NoOrigin PortLineMode USERDOCS DLTARIFFSDOCS ULC/SBREAK DOWNDelivery DateLocationReason Category (NEW)COMMENTSDate Completed Target DateOntime/LateCompleted MonthCountStatus
210887ChennaiCOSCOSEAEDYYYYY44950MAGNA PARKAssigned after Target Date06/01/202305/01/2023LateJan1Completed
310900ChennaiCOSCOSEAJEYYYYY44953MAGNA PARKMissing Invoice783789 wrong info on inv - chased again 09/0110/01/202312/01/2023On TimeJan1Completed
410909Nhava ShevaMSCSEAEDYYYYY44953MAGNA PARK06/01/202312/01/2023On TimeJan1Completed
510916MundraMSCSEAEDYYYYY44953FGS10/01/202312/01/2023On TimeJan2Completed
610916MundraMSCSEAEDYYYYY44953FGS10/01/202312/01/2023On TimeJan2Completed
710939DamiettaHapagSEAJEYYYYY44953MAGNA PARKMissing DeclarationOnly fed in on 12/01 and added 16/01 to adhoc - NEED EUR1 - emailed supplier 18/01/202312/01/2023LateJan1Completed
810939DamiettaHapagSEAJEYYYYY44956MAGNA PARKMissing DeclarationOnly fed in on 12/01 and added 16/01 to adhoc - NEED EUR1 - emailed supplier 18/01/202312/01/2023LateJan1Completed
910939DamiettaHapagSEAJEYYYYY44956MAGNA PARKMissing DeclarationOnly fed in on 12/01 and added 16/01 to adhoc - NEED EUR1 - emailed supplier 18/01/202312/01/2023LateJan1Completed
1010939DamiettaHapagSEAJEYYYYY44953MAGNA PARKMissing DeclarationOnly fed in on 12/01 and added 16/01 to adhoc - NEED EUR1 - emailed supplier 18/01/202312/01/2023LateJan1Completed
1110939DamiettaHapagSEAJEYYYYY44956MAGNA PARKMissing DeclarationOnly fed in on 12/01 and added 16/01 to adhoc - NEED EUR1 - emailed supplier 18/01/202312/01/2023LateJan1Completed
1210901NingboMSCSEAEDYYYYY44957MAGNA PARK05/01/202315/01/2023On TimeJan1Completed
1310901NingboMSCSEAEDYYYYY44960MAGNA PARK05/01/202315/01/2023On TimeJan1Completed
1410912XiamenMSCSEAEDYYYYY44957MAGNA PARK11/01/202315/01/2023On TimeJan1Completed
1510931MundraCOSCOSEASOYYYYY44958FGS12/01/202315/01/2023On TimeJan2Completed
1610931MundraCOSCOSEASOYYYYY44958FGS12/01/202315/01/2023On TimeJan2Completed
1710884Ho Chi MinhMSCSEAEDYYYYY44957MAGNA PARK03/01/202308/03/2023On TimeJan1Completed
1810882ShanghaiYang MSEASOYYYYY44964MAGNA PARK03/01/202316/01/2023On TimeJan1Completed
1910882ShanghaiYang MSEASOYYYYY44964MAGNA PARK03/01/202318/05/2023On TimeJan1Completed
Sheet1


Target Workbook.

Archive 2023.xlsb
NOPQRSTUVWXYZAAABACADAEAFAG
1File NoOrigin PortLineMode USERDOCS DLTARIFFSDOCS ULC/SBREAK DOWNDelivery DateLocationReason Category (NEW)COMMENTSDate Completed Target DateOntime/LateCompleted MonthCountStatus
2
3
4
5
6
7
8
9
10
11
12
Archive SEA
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
A bit confused that your source data doesn't have any data within 3 months of today but by your description maybe try the code below.

Make sure that you have backup of the source file before testing the code as we are deleting rows.

VBA Code:
Sub Filterit()
    Application.ScreenUpdating = False
 
    With Workbooks("main source.xlsb").Sheets("Sheet1").Range("N1:AG" & Workbooks("main source.xlsb").Sheets("Sheet1").Range("AB" & Rows.Count).End(xlUp).Row)
 
        .AutoFilter 15, "<" & DateAdd("m", -3, CDate(Date))
     
        On Error Resume Next
     
        With .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
            .Copy 
            Workbooks("Archive 2023.xlsb").Sheets("Archive SEA").Range("N" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlValues
            .EntireRow.Delete
        End With
     
        On Error GoTo 0
        Workbooks("main source.xlsb").Sheets("Sheet1").ShowAllData
 
    End With
 
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Hi,

Thanks so much for this, yeah I was very limited to the amount of data I could capture with XL2BB so couldn't add all of it.

I tested and it worked with the current details, however as I tried to extended the range to A1:AG, it stopped working,


VBA Code:
With Workbooks("main source.xlsb").Sheets("Sheet1").Range("A1:AG" & Workbooks("main source.xlsb").Sheets("Sheet1").Range("AB" & Rows.Count).End(xlUp).Row)

I think I also needed to change the N to A here right?

VBA Code:
.Copy Workbooks("Archive 2023.xlsb").Sheets("Archive SEA").Range("N" & Rows.Count).End(xlUp).Offset(1)

Another thing I noticed is that as I had to convert the main source to a table as I will be using this data to pull out a power query report. I think it stopped working too, I had a debug error on this.
VBA Code:
On Error GoTo 0
        Sheets("Sheet1").ShowAllData

Is there a way to make it work so it copies from a table (main source) and paste is in another table (archive)?

I'm including links to the full file.

Archive 2023
Main source

Sorry for all the questions.
 
Upvote 0
I tested and it worked with the current details, however as I tried to extended the range to A1:AG
I don't have time to rewrite for a table tonight (it is totally different coding for how you reference the table and the ranges)

For a normal range change from A1:AG What column are the dates in now (not downloading from links as I don't need them for changing a range)?
 
Upvote 0
I don't have time to rewrite for a table tonight (it is totally different coding for how you reference the table and the ranges)

For a normal range change from A1:AG What column are the dates in now (not downloading from links as I don't need them for changing a range)?
The dates are still on AB.

No worries about the tables for tonight I know it's a bit late :)

e:
On Error GoTo 0
Sheets("Sheet1").ShowAllData
That is because you didn't see my edit where added the workbook name
Oh I see, got it now, thanks :)
 
Upvote 0
For a normal range (not a table)

Rich (BB code):
Sub Filterit()
    Application.ScreenUpdating = False
 
    With Workbooks("main source.xlsb").Sheets("Sheet1").Range("A1:AG" & Workbooks("main source.xlsb").Sheets("Sheet1").Range("AB" & Rows.Count).End(xlUp).Row)
 
        .AutoFilter 28, "<" & DateAdd("m", -3, CDate(Date))
     
        On Error Resume Next
     
        With .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
            .Copy
            Workbooks("Archive 2023.xlsb").Sheets("Archive SEA").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlValues
            .EntireRow.Delete
        End With
     
        On Error GoTo 0
        Workbooks("main source.xlsb").Sheets("Sheet1").ShowAllData
 
    End With
 
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
This is just @MARK858's code converted to work with tables.
As in that code it assumes both workbooks are open.
Change the 2 table names to match your table names

VBA Code:
Sub Filterit
    Dim wbMain As Workbook
    Dim wbArch As Workbook
    Dim shtMain As Worksheet
    Dim shtArch As Worksheet
    Dim tblMain As ListObject
    Dim tblArch As ListObject
    Dim lastDataCellArch As Range
    Dim visibleMain As Range
    
    Set wbMain = ThisWorkbook
    Set wbArch = Workbooks("Archive 2023.xlsb")

    Set shtMain = wbMain.Worksheets("Main")
    Set shtArch = wbArch.Worksheets("Archive SEA")
    
    Set tblMain = shtMain.ListObjects("tblMain")            '<--- change table name to your table name
    Set tblArch = shtArch.ListObjects("Table1")             '<--- change table name to your table name
    
    Set lastDataCellArch = tblArch.ListColumns(1).Range.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
    Application.ScreenUpdating = False
    
    If tblMain.ShowAutoFilter = True Then
        tblMain.AutoFilter.ShowAllData
    End If
    
    If tblArch.ShowAutoFilter = True Then
        tblArch.AutoFilter.ShowAllData
    End If
    
    With tblMain
        .Range.AutoFilter Field:=28, Criteria1:="<" & DateAdd("m", -3, CDate(Date))
        If .Range.SpecialCells(xlCellTypeVisible).Rows.Address = .Range.Rows(1).Address Then
            MsgBox "The only visible row is the header row"
        Else
            Application.DisplayAlerts = False
            With .DataBodyRange.SpecialCells(xlCellTypeVisible)
                .Copy
                lastDataCellArch.Offset(1).PasteSpecial Paste:=xlValues
                .Rows.Delete
            End With
            Application.DisplayAlerts = True
        End If
        .AutoFilter.ShowAllData
    End With
 
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
This is just @MARK858's code converted to work with tables.
As in that code it assumes both workbooks are open.
Change the 2 table names to match your table names

VBA Code:
Sub Filterit
    Dim wbMain As Workbook
    Dim wbArch As Workbook
    Dim shtMain As Worksheet
    Dim shtArch As Worksheet
    Dim tblMain As ListObject
    Dim tblArch As ListObject
    Dim lastDataCellArch As Range
    Dim visibleMain As Range
   
    Set wbMain = ThisWorkbook
    Set wbArch = Workbooks("Archive 2023.xlsb")

    Set shtMain = wbMain.Worksheets("Main")
    Set shtArch = wbArch.Worksheets("Archive SEA")
   
    Set tblMain = shtMain.ListObjects("tblMain")            '<--- change table name to your table name
    Set tblArch = shtArch.ListObjects("Table1")             '<--- change table name to your table name
   
    Set lastDataCellArch = tblArch.ListColumns(1).Range.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
    Application.ScreenUpdating = False
   
    If tblMain.ShowAutoFilter = True Then
        tblMain.AutoFilter.ShowAllData
    End If
   
    If tblArch.ShowAutoFilter = True Then
        tblArch.AutoFilter.ShowAllData
    End If
   
    With tblMain
        .Range.AutoFilter Field:=28, Criteria1:="<" & DateAdd("m", -3, CDate(Date))
        If .Range.SpecialCells(xlCellTypeVisible).Rows.Address = .Range.Rows(1).Address Then
            MsgBox "The only visible row is the header row"
        Else
            Application.DisplayAlerts = False
            With .DataBodyRange.SpecialCells(xlCellTypeVisible)
                .Copy
                lastDataCellArch.Offset(1).PasteSpecial Paste:=xlValues
                .Rows.Delete
            End With
            Application.DisplayAlerts = True
        End If
        .AutoFilter.ShowAllData
    End With
 
    Application.ScreenUpdating = True
End Sub
Hello,

thanks you so much for this, I do apologise for the late response I literally have not had the chance to check the code.
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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