VBA to Delete Entire Row if Year is Older than Specified

jski21

Board Regular
Joined
Jan 2, 2019
Messages
155
Office Version
  1. 2016
Platform
  1. Windows
Good day Mr. Excel Team,

Seeking some brief instruction as to how to to delete an entire row of data if the year in the cell of that row precedes the year entered into a message box. Sample data is:

PR07 - Voucher Drawdowns EN Only.xlsx
ABCDEFGHIJKLMNOPQ
1NumberLine ItemProject IDAct IDNumberDateStatusStatusDateSentDateCodePrjctYearTypeRecipient TINPayee TINProgramYearAmountQtr/Yr
21706591221/29/1998Completed01/29/199801/28/1998B97MC3900041997EN11CDBG1998(77,591)1Q98
31742091217942/13/1998Completed02/13/199802/14/1999B97MC3900041997EN22CDBG199836,3491Q98
41742092218502/13/1998Completed02/13/199802/14/2000B97MC3900041997EN33CDBG19983171Q98
51742093220082/13/1998Completed02/13/199802/14/2001B97MC3900041997EN44CDBG19986481Q98
617420941922002/13/1998Completed02/13/199802/14/2002B97MC3900041997EN55CDBG19989501Q98
717420951922022/13/1998Completed02/13/199802/14/2003B97MC3900041997EN66CDBG19981,2661Q98
817420961922012/13/1998Completed02/13/199802/14/2004B97MC3900041997EN77CDBG19985541Q98
91776091223/2/1998Completed03/02/199802/27/2005B97MC3900041997EN88CDBG1998(50)1Q98
101799161218373/11/1998Completed03/13/199803/14/2006B97MC3900041997EN99CDBG1998111,9201Q98
111799162422313/11/1998Completed03/13/199803/14/2007B97MC3900041997EN1010CDBG1998185,6201Q98
121799163222103/11/1998Completed03/13/199803/14/2008B97MC3900041997EN1111CDBG1998572,7501Q98
13179916412822343/11/1998Completed03/13/199803/14/2009B97MC3900041997EN1212CDBG1998425,9041Q98
14179916512922363/11/1998Completed03/13/199803/14/2010B97MC3900041997EN1313CDBG1998455,4961Q98
1517992312922133/11/1998Completed03/13/199803/14/2011B97MC3900041997EN1414CDBG19982,1401Q98
1617992322922123/11/1998Completed03/13/199803/14/2012B97MC3900041997EN1515CDBG19989601Q98
1717992332922113/11/1998Completed03/13/199803/14/2013B97MC3900041997EN1616CDBG1998721Q98
1817992342922193/11/1998Completed03/13/199803/14/2014B97MC3900041997EN1717CDBG19986861Q98
1917992352922203/11/1998Completed03/13/199803/14/2015B97MC3900041997EN1818CDBG19982,5281Q98
2017992362922213/11/1998Completed03/13/199803/14/2016B97MC3900041997EN1919CDBG1998721Q98
2117992372922223/11/1998Completed03/13/199803/14/2017B97MC3900041997EN2020CDBG19985,5611Q98
2217992382922093/11/1998Completed03/13/199803/14/2018B97MC3900041997EN2121CDBG19987501Q98
2317992392922233/11/1998Completed03/13/199803/14/2019B97MC3900041997EN2222CDBG19985,0121Q98
24179923102922253/11/1998Completed03/13/199803/14/2020B97MC3900041997EN2323CDBG1998361Q98
25179923112922083/11/1998Completed03/13/199803/14/2021B97MC3900041997EN2424CDBG19982,5831Q98
26179923122922243/11/1998Completed3/13/199803/14/2022B97MC3900041997EN2525CDBG19983,1011Q98
PR07 - Voucher Drawdowns EN Onl



The date field used would be Column H. Example: If one enters "2022" all the rows with years from 2021 and prior would be deleted.

Thanks in advance for your review, consideration, and assistance.


jski
 
Do you mean something like this:
VBA Code:
If StartRow > 2 Then Rows("2:" & StartRow -1).Delete

So simple but yet so far...

Yes Joe4. Just like that! Thank you for your guidance and help. Posting the full code below for future users:

'Open Input Box and Ask User for a Start Year. (LOCATE FIRST INSTANCE OF StartYear AND DELTE ALL OLDER ROWS)
Columns("O:O").Select

'Turn Off Screen Updating
Application.ScreenUpdating = False

'Display message box and ask user for StartYear
StartYear = Application.InputBox("Please provide a start year:", "StartYear", Type:=1)

'Delete all rows prior to StartYear
StartRow = Columns("O:O").Find(What:=StartYear, After:=[O1], LookIn:=xlValues, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Row
If StartRow > 2 Then Rows("2:" & StartRow - 1).Delete

'Turn On Screen Updating
Application.ScreenUpdating = True
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You are welcome.

Glad you have it working now!
 
Upvote 0
Well done!

The macro recorder helps with the syntax, but generates flabby and clunky code. A cleaner version:

VBA Code:
Sub Test()

    Dim StartYear As String
   
    StartYear = Application.InputBox("Please provide a start year", "StartYear", Type:=1)
   
    Application.ScreenUpdating = False
   
    With Worksheets("Sheet1").ListObjects("PR07Table").DataBodyRange
        .AutoFilter Field:=15, Criteria1:="<" & StartYear
        On Error Resume Next
        Application.DisplayAlerts = False
        .SpecialCells(xlCellTypeVisible).Delete
        Application.DisplayAlerts = True
        On Error GoTo 0
        .AutoFilter
    End With

    Application.ScreenUpdating = True

End Sub
Hi Stephen,

Just giving you a knock as I mistakenly didn't hit the post reply to your message. Sorry about that. Just wanted to keep you in the loop as to the ideas passing through this entire thread. Thanks again for your help!

jski
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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