Macros to Delete Dates not in Current Month

Jones1413

New Member
Joined
Jul 26, 2019
Messages
47
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to create a macro that will delete all rows of data does not have an anniversary date in the current month.

For example, I would like the macro to search through my sheet to find the last row of data and then delete any row that does not have an anniversary date in the current month(May). Then be able to do the same thing next month, delete any row that does not have an anniversary date in the month of June and so on.

Book1
AB
1Employee IDAnniversary Date
214/1/2001
3212/12/1998
436/15/2004
549/15/1994
655/15/2015
763/20/2009
875/27/2019
985/1/2021
10910/13/2013
11106/4/2004
Sheet1
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
You don't need a macro for this...
Try inserting this formula in Sheet2-B2 (or any preferred location):
Excel Formula:
=FILTER(Sheet1!A1:B1000,IFERROR(MONTH(Sheet1!B1:B1000)=MONTH(TODAY()),TRUE))
This assumes the list contains up to 1000 lines; adapt if you have more
 
Upvote 0
try this:
VBA Code:
Sub tst()
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
 inarr = Range(Cells(1, 1), Cells(lastrow, 2))
 Range(Cells(2, 1), Cells(lastrow, 2)) = ""
 outarr = Range(Cells(1, 1), Cells(lastrow, 2))
 Cmonth = Month(Now())
 indi = 2
 For i = 2 To lastrow
  If Month(inarr(i, 2)) = Cmonth Then
   outarr(indi, 1) = inarr(i, 1)
   outarr(indi, 2) = inarr(i, 2)
   indi = indi + 1
  End If
 Next i
Range(Cells(1, 1), Cells(indi, 2)) = outarr
End Sub
 
Upvote 0
Solution
Another option. Try it on a copy of your data.

VBA Code:
Option Explicit
Sub Jones1413()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")
    Dim LRow As Long, LCol As Long, m As Long, i As Long, a, b
    m = Month(Date)
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
    
    a = Range(ws.Cells(2, 2), ws.Cells(LRow, 2))
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
        If Month(a(i, 1)) <> m Then b(i, 1) = 1
    Next i
    
    ws.Cells(2, LCol).Resize(UBound(a)) = b
    i = WorksheetFunction.Sum(ws.Columns(LCol))
    If i > 0 Then
        ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol)).Sort Key1:=ws.Cells(2, LCol), _
        order1:=xlAscending, Header:=xlNo
        ws.Cells(2, LCol).Resize(i).EntireRow.Delete
    End If
End Sub

Before:
Book1
AB
1Employee IDAnniversary Date
214/01/2001
3212/12/1998
436/15/2004
549/15/1994
655/15/2015
763/20/2009
875/27/2019
985/01/2021
10910/13/2013
11106/04/2004
Sheet1


After:
Book1
AB
1Employee IDAnniversary Date
255/15/2015
375/27/2019
485/01/2021
5
Sheet1
 
Upvote 0
try this:
VBA Code:
Sub tst()
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
 inarr = Range(Cells(1, 1), Cells(lastrow, 2))
 Range(Cells(2, 1), Cells(lastrow, 2)) = ""
 outarr = Range(Cells(1, 1), Cells(lastrow, 2))
 Cmonth = Month(Now())
 indi = 2
 For i = 2 To lastrow
  If Month(inarr(i, 2)) = Cmonth Then
   outarr(indi, 1) = inarr(i, 1)
   outarr(indi, 2) = inarr(i, 2)
   indi = indi + 1
  End If
 Next i
Range(Cells(1, 1), Cells(indi, 2)) = outarr
End Sub
This worked great. However, the Anniversary Date is in Column E and my range of data is Column A:F. How would I adjust the above code to account for that?
 
Upvote 0
Very easily done try this:
VBA Code:
Sub tst()
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Lcol = 6    ' Define last column as column 6 (F) or you could use Kevin9999 method
 inarr = Range(Cells(1, 1), Cells(lastrow, Lcol))
 Range(Cells(2, 1), Cells(lastrow, Lcol)) = ""
 outarr = Range(Cells(1, 1), Cells(lastrow, Lcol))
 Cmonth = Month(Now())
 indi = 2
 For i = 2 To lastrow
  If Month(inarr(i, 5)) = Cmonth Then  ' check column 5 (E)
  ' set up loop to copy 6 columns rather that 6 similar statements
   For j = 1 To 6
   outarr(indi, j) = inarr(i, j)
   Next j
   indi = indi + 1
  End If
 Next i
Range(Cells(1, 1), Cells(indi, Lcol)) = outarr
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,802
Messages
6,181,054
Members
453,014
Latest member
Chris258

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