Filter by Date macro

Andresleo47

Board Regular
Joined
Oct 29, 2008
Messages
132
Hi Dear Community, I am trying to build a macro that Filters by date all the months before the month we're in. For example, we're in September 2022. I want to filter by date all the dates before August 2022 and then I want to delete those rows.

This is my code so far, and the Sub I'm talking about is the one called Sub FilterDates()

Thanks,


VBA Code:
Sub Path1()
 With Application.FileDialog(msoFileDialogFilePicker)
   If .Show Then Sheets("Import Files").TextBox1.Text = .SelectedItems(1)
 End With
End Sub

  
Sub Path2()
 With Application.FileDialog(msoFileDialogFilePicker)
   If .Show Then Sheets("Import Files").TextBox2.Text = .SelectedItems(1)
 End With
End Sub

Sub Path3()
 With Application.FileDialog(msoFileDialogFilePicker)
   If .Show Then Sheets("Import Files").TextBox3.Text = .SelectedItems(1)
 End With
End Sub



Private Sub CommandButton1_Click()

'Code done by: Andres Veloza for HCM Works!
'
    Dim ClosedWorkbookSheetName As String
    Dim FileExtention           As String
    Dim NewWorkbookName         As String
    Dim SheetWithTextBoxes      As String
    Dim ClosedSourceWorkbook    As Workbook
'
    FileExtention = ".xlsx"                                                                 ' <--- Set this to the file extention of workbook to be created
    NewWorkbookName = "ThisWeek"                                                            ' <--- Set this to the name of the workbook to be created
    ClosedWorkbookSheetName = "report"                                                      ' <--- Set this to the sheet name in the closed workbook
    SheetWithTextBoxes = "Import Files"                                                     ' <--- Set this to the sheet name that contains the TextBoxes
'
    Workbooks.Add.SaveAs Filename:=NewWorkbookName & FileExtention                          ' Create/Name/save New Workbook


   ThisWorkbook.Activate                                                                   ' Reactivate this workbook
'
    Application.ScreenUpdating = False                                                      ' Turn ScreenUpdating off
'
    Set ClosedSourceWorkbook = Workbooks.Open(Sheets(SheetWithTextBoxes).TextBox3.Value)    ' Get value from TextBox to open the source file
'
'   Copy sheet to beginning of the newly created workbook
    ClosedSourceWorkbook.Sheets(ClosedWorkbookSheetName).Copy Before:=Workbooks(NewWorkbookName & FileExtention).Sheets(1)
    ClosedSourceWorkbook.Close SaveChanges:=False                                           ' Close the source workbook
'





'
    ThisWorkbook.Activate                                                                   ' Reactivate this workbook
'
    Application.ScreenUpdating = False                                                      ' Turn ScreenUpdating off
'
    Set ClosedSourceWorkbook = Workbooks.Open(Sheets(SheetWithTextBoxes).TextBox2.Value)    ' Get value from TextBox to open the source file
'
'   Copy sheet to beginning of the newly created workbook
    ClosedSourceWorkbook.Sheets(ClosedWorkbookSheetName).Copy Before:=Workbooks(NewWorkbookName & FileExtention).Sheets(1)
    ClosedSourceWorkbook.Close SaveChanges:=False                                           ' Close the source workbook
'
    ThisWorkbook.Activate                                                                   ' Reactivate this workbook
'
    Set ClosedSourceWorkbook = Workbooks.Open(Sheets(SheetWithTextBoxes).TextBox1.Value)    ' Get value from TextBox to open the source file
'
'   Copy sheet to beginning of the newly created workbook
    ClosedSourceWorkbook.Sheets(ClosedWorkbookSheetName).Copy Before:=Workbooks(NewWorkbookName & FileExtention).Sheets(1)
    ClosedSourceWorkbook.Close SaveChanges:=False                                           ' Close the source workbook
'
    Application.DisplayAlerts = False                                                       ' Turn off DisplayAlerts to avoid sheet deletion warning
    Sheets("Sheet1").Delete                                                                 ' Delete Sheet1 from the newly created workbook
    Application.DisplayAlerts = True                                                        ' Turn DisplayAlerts back on
'
'   ActiveWorkbook.Close True                                                                ' Save newly created file and close
    ActiveWorkbook.Save
    Application.ScreenUpdating = True                                                       ' Turn ScreenUpdating back on
'End

Worksheets("report (2)").Activate
Range("A1").EntireRow.Delete

Call InsertPivotTable

End Sub


Sub InsertPivotTable()



'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim lastRow As Long
Dim LastCol As Long



'Insert a New Blank Worksheet
On Error Resume Next
Worksheets("PivotTable").Delete
On Error GoTo 0
Application.DisplayAlerts = False
'Worksheets("PivotTable").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("report (2)")

lastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(lastRow, LastCol)

'Define Pivot Cache
Set PTable = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="PivotTable")

'Insert Blank Pivot Table
'Set PTable = PCache.CreatePivotTable _
'(TableDestination:=PSheet.Cells(0, 0), TableName:="PivotTable")

'Insert Row Fields
With ActiveSheet.PivotTables("PivotTable").PivotFields("Work Order ID")
.Orientation = xlRowField
.Position = 1
End With

'Insert Data Field
With ActiveSheet.PivotTables("PivotTable").PivotFields("Total Approved Billable Hours")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Total Approved Billable Hours "
End With

With ActiveSheet.PivotTables("PivotTable").PivotFields("Spend to Date")
.Orientation = xlDataField
.Position = 2
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Spend to Date "
End With

Worksheets("report (3)").Activate

Call ChangeFirstRow

End Sub

Sub ChangeFirstRow()                                                                         'This code is to put the right format in Row 1

Cells(1, 1).Value = "Master Worker Record ID"
Cells(1, 2).Value = "Job Posting ID"
Cells(1, 3).Value = "Work Order ID"
Cells(1, 4).Value = "Worker"
Cells(1, 5).Value = "Legal Entity"
Cells(1, 6).Value = "Cost Center"
Cells(1, 7).Value = "Work Order Status"
Cells(1, 8).Value = "Supplier"
Cells(1, 9).Value = "Job Posting Title"
Cells(1, 10).Value = "Real Start Date"
Cells(1, 11).Value = "Work Order Start Date"
Cells(1, 12).Value = "Worker Start Date"
Cells(1, 13).Value = "First Work Order End Date"
Cells(1, 14).Value = "Latest Work Order End Date"
Cells(1, 15).Value = "Spend to Date"
Cells(1, 16).Value = "Hours per Week"
Cells(1, 17).Value = "Cumulative Committed Spend"
Cells(1, 18).Value = "Total Approved Billable Hours"
Cells(1, 19).Value = "Total Estimated Hours"
Cells(1, 20).Value = "Business Unit"
Cells(1, 21).Value = "Hiring Manager"
Cells(1, 22).Value = "Work Order Owner Username"
Cells(1, 23).Value = "Invoice Approver"
Cells(1, 24).Value = "Current Bill Rate [ST/Hr]"
Cells(1, 25).Value = "Site"
Cells(1, 26).Value = "Worker Status"
Cells(1, 27).Value = "Revision #"
Cells(1, 28).Value = "Status"
Cells(1, 29).Value = "Worker Email"

Range("A1:AC1").Font.Bold = True
Range("A1:AC1").Select
With Selection
   .HorizontalAlignment = xlCenter
   .VerticalAlignment = xlCenter
  End With
Range("A1").Font.Size = 9

Call ShiftColumns

End Sub


Sub ShiftColumns()

Dim Fnd As Range                                                                                'This code moves the "Worker" column
   Set Fnd = Range("2:2").Find("Worker", , , xlWhole, , xlNext, False, , False)
   If Not Fnd Is Nothing Then
      Range(Fnd.Offset(, -1), Cells(Rows.Count, Fnd.Offset(, -1).Column)).Delete xlToLeft
   End If
   
   
Dim Fnd2 As Range                                                                               'This code moves the "Work Order Status" column
Set Fnd2 = Range("2:2").Find("Work Order Status", , , xlWhole, , xlNext, False, , False)
If Not Fnd2 Is Nothing Then
Range(Fnd2, Cells(Rows.Count, Fnd2.Column)).Resize(, 2).Insert xlToRight
End If


Dim Fnd3 As Range, Fnd4 As Range                                                                'This code moves the "Legal Entity" column
   Set Fnd3 = Range("2:2").Find("Legal Entity", , , xlWhole, , xlNext, False, , False)
   Set Fnd4 = Range("1:1").Find("Legal Entity", , , xlWhole, , xlNext, False, , False)
   If Not Fnd3 Is Nothing And Not Fnd4 Is Nothing Then
      Range(Fnd3, Cells(Rows.Count, Fnd3.Column)).Cut Fnd4.Offset(1)
   End If

Dim Fnd5 As Range, Fnd6 As Range                                                                'This code moves the "Cost Center" column
   Set Fnd5 = Range("2:2").Find("Cost Center", , , xlWhole, , xlNext, False, , False)
   Set Fnd6 = Range("1:1").Find("Cost Center", , , xlWhole, , xlNext, False, , False)
   If Not Fnd5 Is Nothing And Not Fnd6 Is Nothing Then
      Range(Fnd5, Cells(Rows.Count, Fnd5.Column)).Cut Fnd6.Offset(1)
   End If

 Dim Fnd7 As Range
   Set Fnd7 = Range("1:1").Find("Hiring Manager", , , xlWhole, , xlNext, False, , False)
   If Not Fnd7 Is Nothing Then
      Fnd7.Offset(1) = "Hiring Manager"
   End If

Dim Fnd8 As Range
   Set Fnd8 = Range("1:1").Find("Invoice Approver", , , xlWhole, , xlNext, False, , False)
   If Not Fnd8 Is Nothing Then
      Fnd8.Offset(1) = "Invoice Approver"
   End If


Call PopulateHMandIA_v02

End Sub


Sub PopulateHMandIA_v02()

  Dim ws As Worksheet
    Dim wb As Workbook
    Dim UserHdg As Range
    Dim rngUserName As Range
    Dim MyLookup As Range
    Dim wbApprov As Workbook
    Dim wsApprovers As Worksheet
    Dim HM_Hdg As Range
    Dim rngHM As Range
    Dim lastRow As Long
   
    Set wb = Workbooks("ThisWeek.xlsx")
    Set ws = wb.Worksheets("report (3)")
                                                  
    Set wbApprov = ThisWorkbook                         '<--- approvers list in THIS workbook
    Set wsApprovers = wbApprov.Worksheets("Approvers")
   
    With wsApprovers
        Set MyLookup = .Range("A1:C" & .Cells(Rows.Count, "A").End(xlUp).Row)   ' XXX Added Column C
    End With
   
    Set UserHdg = ws.Range("1:1").Find("Work Order Owner Username", , , xlWhole, , xlNext, False, , False)
    If Not UserHdg Is Nothing Then
        Set rngUserName = UserHdg.Offset(2)
        lastRow = ws.Cells(Rows.Count, rngUserName.Column).End(xlUp).Row
    Else
        Exit Sub
    End If
   
    Set HM_Hdg = ws.Range("1:1").Find("Hiring Manager", , , xlWhole, , xlNext, False, , False)
    With ws
        Set rngHM = .Range(HM_Hdg.Offset(2), .Cells(lastRow, HM_Hdg.Column))
    End With
     
    rngHM.FormulaR1C1 = "=VLookup(" & "RC" & rngUserName.Column & "," & MyLookup.Address(1, 1, 0, 1) & ", 2, False)"
    rngHM.Value = rngHM.Value                       '<--- Removes formulas - comment out if you want to keep them
    
    With wsApprovers
        Set MyLookup = .Range("A1:C" & .Cells(Rows.Count, "A").End(xlUp).Row)
    End With
    
    ' XXX Added look for Invoice Approver column
    Set HM_Hdg = ws.Range("1:1").Find("Invoice Approver", , , xlWhole, , xlNext, False, , False)
    With ws
        Set rngHM = .Range(HM_Hdg.Offset(2), .Cells(lastRow, HM_Hdg.Column))
    End With
    rngHM.FormulaR1C1 = "=VLookup(" & "RC" & rngUserName.Column & "," & MyLookup.Address(1, 1, 0, 1) & ", 3, False)"
    rngHM.Value = rngHM.Value                       '<--- Removes formulas - comment out if you want to keep them
    
    HM_Hdg.Offset(1).EntireRow.Delete
    
    Call PopulateWorkerEmail


End Sub

Sub PopulateWorkerEmail()

Worksheets("report (3)").Select
Dim Formula As Long
Range("AC2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-26],report!C[-26]:C,27,0)"
Range("AC2").Select
Selection.AutoFill Destination:=Range("AC2:AC658")
Range("N1").Select
Worksheets("report (3)").Range("A1").AutoFilter

Call FilterDates

End Sub

Sub FilterDates()

Current_Date = Format(Date, "mm/yyyy")

Range("A4:A10").AutoFilter
    Range("A4:A10").AutoFilter Field:=1, Criteria1:=">=" & Current_Date, _
                     Operator:=xlAnd, Criteria2:="<" & Current_Date + 1
                     
  

'Call ChangeTabsNames
End Sub


Sub ChangeTabsNames()

Sheets("report").Name = "Last Week's Report"
Sheets("report (2)").Name = "Spend by Contractor"
Sheets("report (3)").Name = "This Week's Report"

'Call File_Saver

End Sub

Sub File_Saver()

' Some variables
Dim IntialName As String
Dim fileSaveName As Variant

'A catch to give a default name of what's in cell A1
InitialName = Range("A1")

'Automatic generation of month day, year file name, 4 m's will give the full month, 1 d will give 2 instead of 02 and yy will give you a 2 digit year. This includes spaces and a comma.
YourFile = Format(Date, "yyyy") & "-" & Format(Date, "mm") & "-" & Format(Date, "dd") & ".xlsx"

'File save dialog opener
fileSaveName = Application.GetSaveAsFilename(InitialFileName:=IntialName & "Active Contractor - Cost Center Legal Entity - " & " " & _
YourFile, fileFilter:="Excel Files (*.xlsx), *.xlsx")

End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Wow. That is a lot of code to slog! I imagine code that sorts the table in question by date then all rows that are for dates before the current month are deleted. Would that work? If so provide some data to work with using XL2BB. See here: XL2BB - Excel Range to BBCode
 
Upvote 0
Yes, kind of...Quick question. Will all the users of the resulting macro be able to use it? Not sure to have worked with XL2BB before. But would be happy to share
 
Upvote 0
Will all the users of the resulting macro be able to use it?
I am not sure how to answer your quick question except to say that anyone who has a workbook with the code in it will be able to use the code. Is there another level of detail to your question?
 
Upvote 0
Oh I just noticed it's an Add-In just to make it easier here lol. Sorry. I made the process, and here's the result of the copied rows that I would like to filter

ThisWeek.xlsx
N
1Latest Work Order End Date
205-13-2022
307-31-2021
404-16-2021
507-31-2021
604-16-2021
707-31-2021
808-16-2019
910-31-2019
1007-06-2019
1106-12-2020
1212-31-2022
1304-01-2019
1404-30-2020
1505-24-2019
1605-10-2019
1706-30-2021
1804-01-2019
1908-30-2019
2012-31-2020
2106-30-2023
2212-31-2020
2305-08-2023
2403-27-2022
2512-31-2022
2612-31-2020
2712-31-2022
2803-31-2021
2908-21-2020
3012-16-2022
3103-25-2022
3212-31-2022
3303-27-2022
3412-31-2022
3509-12-2019
3606-12-2020
3704-01-2019
3805-22-2020
3906-30-2021
4010-17-2019
4112-31-2020
4206-30-2019
4312-13-2019
4402-28-2020
4503-27-2020
4605-29-2020
4702-18-2021
4806-30-2021
4912-31-2022
5009-30-2021
5112-31-2022
5206-30-2022
5306-30-2019
5403-27-2022
5512-31-2022
5610-29-2021
5709-30-2021
5812-31-2022
5904-01-2019
6006-14-2019
6109-10-2021
6204-01-2019
6303-27-2022
6412-31-2022
6503-12-2021
6611-12-2021
6707-31-2020
6809-27-2019
6908-16-2019
7004-02-2019
7110-08-2021
7204-01-2019
7301-21-2020
7409-02-2020
7510-27-2019
7606-30-2019
7704-01-2019
7803-31-2021
7906-30-2019
8012-31-2022
8103-25-2022
8212-31-2022
8309-10-2019
8405-06-2019
8512-16-2022
8604-15-2019
8707-25-2022
8806-14-2019
8902-05-2021
9006-30-2023
9106-28-2019
9206-23-2019
9312-31-2022
9404-01-2019
9506-30-2019
9604-01-2019
9707-31-2019
9806-28-2019
9906-23-2019
10008-24-2020
10112-30-2022
10209-27-2019
10304-03-2020
10412-06-2019
10509-17-2021
10602-19-2021
10712-31-2022
10801-17-2020
10907-25-2022
11004-01-2019
11107-07-2019
11207-25-2022
11306-30-2019
11410-21-2019
11512-31-2022
11608-31-2021
11706-23-2019
11806-30-2019
11906-30-2023
12012-31-2022
12104-01-2019
12210-30-2020
12312-12-2019
12404-01-2019
12507-18-2019
12606-28-2019
12712-17-2021
12812-31-2022
12902-12-2021
13012-31-2020
13103-27-2022
13212-31-2022
13304-01-2019
13403-27-2022
13512-31-2022
13604-01-2019
13704-01-2019
13810-25-2019
13912-13-2019
14003-31-2021
14112-31-2022
14203-27-2022
14312-31-2022
14403-25-2022
14512-31-2022
14606-30-2019
14703-27-2022
14812-31-2022
14906-14-2019
15012-31-2022
15103-27-2022
15212-30-2022
15303-27-2022
15412-31-2022
15503-13-2020
15601-21-2022
15703-27-2022
15812-30-2022
15910-18-2019
16001-29-2021
16101-29-2020
16212-20-2019
16302-28-2021
16406-10-2022
16512-30-2022
16606-30-2019
16706-30-2019
16812-31-2022
16906-28-2019
17012-31-2022
17112-31-2019
17205-17-2019
17304-03-2019
17406-30-2020
17502-11-2020
17612-31-2022
17706-30-2021
17807-20-2021
17903-31-2022
18007-30-2021
18109-23-2019
18204-01-2019
18312-31-2021
18403-31-2020
18506-30-2021
18603-27-2022
18712-31-2022
18807-10-2019
18906-28-2019
19011-22-2019
19101-31-2021
19204-20-2019
19307-30-2021
19405-07-2021
19512-31-2022
19606-21-2019
19707-05-2019
19812-16-2022
19906-06-2019
20012-24-2019
20110-30-2020
20212-31-2019
20311-15-2019
20403-27-2022
20512-30-2022
20605-21-2019
20706-30-2020
20812-31-2022
20909-11-2020
21007-31-2019
21103-31-2023
21210-08-2021
21303-31-2021
21403-01-2021
21512-23-2020
21609-27-2021
21706-28-2019
21812-30-2022
21905-06-2022
22012-30-2022
22108-09-2021
22205-01-2020
22303-27-2022
22412-30-2022
22504-19-2021
22609-30-2019
22709-30-2019
22804-22-2022
22905-31-2023
23009-27-2019
23106-30-2023
23208-09-2019
23312-13-2019
23411-01-2019
23506-18-2021
23607-02-2019
23709-09-2021
23806-30-2020
23907-30-2021
24012-31-2019
24111-30-2019
24210-01-2019
24307-25-2019
24404-22-2022
24512-30-2022
24610-18-2019
24707-25-2022
24807-25-2022
24910-04-2019
25012-31-2020
25102-24-2022
25206-07-2020
25303-25-2022
25412-31-2022
25508-08-2021
25606-12-2020
25706-30-2021
25802-17-2021
25911-22-2019
26010-18-2019
26101-03-2020
26203-27-2022
26312-31-2022
26411-29-2019
26503-12-2021
26609-30-2019
26707-31-2020
26808-12-2020
26903-31-2021
27006-19-2020
27110-16-2019
27210-30-2020
27312-13-2019
27403-27-2020
27511-20-2020
27612-24-2019
27707-03-2020
27803-27-2022
27912-31-2022
28011-06-2020
28101-29-2020
28201-31-2020
28301-31-2020
28411-29-2019
28511-25-2019
28612-17-2021
28712-31-2022
28812-17-2019
28901-29-2021
29011-22-2021
29104-10-2020
29212-09-2022
29305-15-2020
29412-31-2019
29501-01-2020
29601-01-2020
29711-08-2019
29812-31-2022
29912-31-2019
30008-02-2021
30111-04-2019
30207-09-2021
30312-11-2020
30407-25-2022
30503-31-2021
30609-29-2020
30711-24-2023
30812-13-2019
30908-13-2021
31012-31-2022
31107-20-2021
31212-31-2022
31307-10-2020
31405-22-2020
31501-17-2020
31611-12-2021
31705-08-2020
31803-26-2022
31906-30-2023
32003-27-2022
32112-31-2022
32203-13-2020
32304-30-2021
32411-06-2020
32501-03-2020
32610-31-2020
32712-31-2020
32807-15-2021
32903-31-2023
33012-31-2022
33112-11-2020
33211-06-2020
33302-18-2020
33412-31-2020
33507-22-2022
33606-30-2020
33703-30-2020
33803-19-2021
33912-31-2022
34012-21-2019
34112-31-2022
34212-21-2019
34312-21-2019
34412-31-2022
34510-30-2020
34612-31-2022
34712-31-2022
34806-30-2021
34904-08-2022
35012-31-2022
35103-27-2022
35205-27-2022
35304-17-2020
35408-21-2020
35511-06-2020
35610-30-2020
35704-09-2021
35809-25-2020
35902-10-2020
36009-18-2020
36112-31-2023
36206-30-2021
36306-05-2020
36408-21-2020
36504-01-2021
36608-11-2021
36708-31-2021
36805-11-2020
36905-31-2021
37009-25-2020
37108-31-2021
37208-26-2020
37306-03-2022
37403-27-2022
37512-31-2022
37603-31-2021
37704-30-2021
37810-16-2020
37909-11-2020
38012-31-2022
38110-16-2020
38212-31-2022
38312-31-2021
38410-02-2020
38508-14-2020
38605-21-2021
38706-24-2022
38802-16-2021
38906-01-2020
39012-31-2021
39103-23-2020
39212-23-2021
39303-27-2022
39412-31-2022
39512-31-2022
39603-31-2021
39704-30-2021
39811-06-2020
39906-30-2023
40008-30-2021
40112-31-2020
40212-31-2021
40312-09-2021
40401-04-2021
40509-04-2020
40602-16-2021
40712-31-2021
40808-17-2020
40902-17-2021
41011-27-2020
41106-30-2023
41210-16-2020
41312-30-2022
41403-27-2022
41512-31-2022
41611-05-2021
41701-15-2021
41801-28-2022
41909-21-2020
42006-30-2021
42106-12-2021
42201-01-2022
42303-19-2021
42412-01-2020
42512-31-2020
42611-16-2022
42712-31-2020
42805-31-2021
42902-28-2022
43012-18-2020
43106-15-2021
43204-30-2021
43304-30-2021
43405-27-2022
43504-30-2021
43603-31-2022
43707-05-2021
43805-31-2022
43911-01-2022
44002-26-2021
44106-30-2021
44201-21-2022
44310-15-2022
44403-27-2022
44502-10-2023
44603-20-2021
44701-03-2022
44812-31-2022
44911-05-2021
45011-05-2021
45112-31-2022
45201-29-2021
45306-10-2022
45401-13-2021
45512-31-2021
45603-12-2021
45712-31-2020
45807-31-2021
45903-26-2021
46004-14-2021
46104-30-2021
46204-30-2021
46312-31-2022
46403-05-2021
46506-30-2022
46609-10-2021
46703-27-2022
46808-20-2021
46907-25-2022
47001-13-2022
47102-23-2023
47206-30-2022
47304-01-2022
47403-03-2023
47504-01-2022
47610-13-2021
47712-31-2021
47812-31-2022
47905-31-2022
48007-05-2022
48109-30-2022
48210-25-2021
48312-17-2021
48402-11-2022
48512-30-2022
48610-12-2021
48710-18-2022
48812-15-2023
48910-25-2021
49001-31-2023
49101-21-2022
49212-30-2022
49312-07-2021
49405-17-2022
49512-17-2021
49607-29-2022
49701-07-2022
49812-31-2022
49912-31-2022
50012-30-2022
50111-15-2022
50207-29-2022
50305-13-2022
50402-04-2022
50512-31-2022
50603-11-2022
50702-28-2022
50810-27-2023
50904-29-2022
51011-25-2022
51112-31-2022
51209-28-2022
51311-29-2021
51403-25-2022
51506-03-2022
51604-22-2022
51712-01-2021
51812-13-2021
51912-31-2022
52012-13-2021
52105-31-2022
52211-28-2022
52311-28-2022
52412-31-2022
52504-11-2022
52601-28-2022
52711-30-2022
52801-09-2023
52912-03-2022
53006-14-2022
53106-10-2022
53201-04-2023
53301-14-2022
53412-31-2022
53501-10-2023
53602-03-2023
53704-22-2022
53812-31-2022
53904-22-2022
54012-31-2022
54102-24-2022
54205-14-2022
54306-01-2022
54406-09-2022
54502-13-2023
54602-14-2023
54706-23-2019
54806-30-2020
54907-16-2021
55006-05-2020
55106-23-2019
55207-01-2019
55304-01-2019
55406-30-2020
55506-30-2019
55612-31-2022
55712-31-2022
55803-27-2022
55912-31-2022
56006-07-2020
56103-27-2022
56203-27-2022
56307-25-2022
56412-31-2022
56503-31-2020
56606-19-2020
56706-30-2022
56812-30-2022
56907-22-2022
57005-12-2022
57106-27-2022
57207-22-2022
57302-24-2023
57404-26-2022
57512-30-2022
57603-31-2023
57712-23-2022
57812-31-2022
57912-31-2022
58007-14-2022
58104-25-2022
58206-10-2022
58304-18-2022
58412-31-2022
58512-31-2022
58604-15-2023
58704-27-2022
58806-30-2023
58912-16-2022
59012-23-2022
59112-30-2022
59205-22-2023
59307-08-2022
59410-14-2022
59507-29-2022
59607-01-2022
59712-31-2022
59811-29-2022
59911-30-2022
60005-30-2022
60106-06-2022
60212-31-2022
60312-31-2022
60407-31-2022
60512-31-2022
60612-30-2022
60712-31-2022
60812-30-2022
60912-31-2022
61012-31-2022
61112-31-2022
61209-23-2022
61312-31-2022
61412-16-2022
61512-30-2022
61612-15-2022
61711-26-2022
61812-31-2022
61907-25-2023
62012-30-2022
62112-31-2022
62201-27-2023
62312-31-2022
62407-17-2023
62507-19-2022
62607-08-2023
62712-31-2022
62801-31-2023
62908-04-2022
63002-03-2023
63108-08-2023
63208-15-2023
63308-18-2022
63408-18-2022
63501-31-2023
63608-15-2022
63709-06-2022
63806-30-2023
63906-30-2023
64006-30-2023
64103-03-2023
64212-26-2022
64312-30-2022
64412-30-2022
64512-31-2022
64602-03-2023
64712-16-2023
64802-10-2023
64909-06-2023
65012-31-2022
65101-13-2023
65206-30-2023
65312-31-2022
65406-30-2023
65503-31-2023
65603-31-2023
65710-06-2022
65812-31-2022
report (3)
 
Upvote 0
So your data is nothing more than a series of dates? Are their other related columns?
 
Upvote 0
It's a little unclear as to what you actually want. On the one hand you say:
Filters by date all the months before the month we're in.

But then you say:
we're in September 2022. I want to filter by date all the dates before August 2022

So I'm not sure if (using your example) you want to include August (and earlier) dates deleted or not?

Either way, I've given you the choice of each in the code below. Just change the sheet name to suit; I've assumed you're using column N; and remember to test it on a copy of your data first.
Because we are now in October, September is regarded as the first month to delete (or before) in the code.

VBA Code:
Option Explicit
Sub Andresleo47()
    Dim ws As Worksheet
    Set ws = Worksheets("report (3)")   '<< change to actual sheet name
    Dim ThisMonth As Date, Rng As Range
    
    ThisMonth = WorksheetFunction.EoMonth(Date, -1) '<< use this if you want to delete from last month back
    'ThisMonth = WorksheetFunction.EoMonth(Date, -2) '<< use this if you want to delete from month before last month back
    Set Rng = ws.Range("N1", ws.Cells(Rows.Count, "N").End(xlUp))
    
    With Rng
    .AutoFilter 1, "<=" & CLng(ThisMonth)
    .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
    .AutoFilter
    End With

End Sub


Ignore this blank comment below.
 
Upvote 0

Forum statistics

Threads
1,224,809
Messages
6,181,076
Members
453,020
Latest member
mattg2448

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