I have a unique situation not covered by an other article I can find here. I have a workbook of tens of thousands of lines, but they're all essentially like this:
There's a whole lot of "stuff" going on in the workbook and data is constantly added, but the crux of my issue is that I need a piece of code to be able to keep a certain number of the most recent instances of the data (let's say 2) and remove the rest. I don't deal with dates in VBA very often so I wish I could "show my work" thus far, but I truly don't know where to start.
In plain English: Count the number of unique dates in column D. If that number is > 2, THEN delete rows where the date is older than the 2 most recent dates.
Again, I apologize for not having any work to show thus far. I truly have "writers' block" on this one. Any help is appreciated!
UPDATE: With the help in the comments I've written the following to do the first step of finding the 2nd most recent date on my real data sheet (35000+ rows) where the date column is P. I must be doing something wrong because as I track the value of OldMax in the locals window it only returns the most recent date no matter what I put in for the number in Large(DateRange,whatever number)
. Hmmmmm....
Sub Remove_Old_Data()
Dim wks As Worksheet
Dim OldMax As String
Dim DateRange As Range
Dim lrow As Long
Set wks = ThisWorkbook.Worksheets("X-AotA")
lrow = wks.Cells(Rows.Count, "P").End(xlUp).Row
Set DateRange = wks.Range("P2:P" & lrow)
OldMax = Application.WorksheetFunction.Large(DateRange, 2)
End Sub