Delete Rows by Speciic Date Set in the Code

lefty365

New Member
Joined
Jan 5, 2015
Messages
19
I have a spreadsheet with rows of data sorted by date. I need to delete rows older than a specific date I want to set in the code, not an input box. Let's say 12/07/2018. The dates are currently in Column F. I'd like to set the column in the code as well. It could change. There are hundreds of rows but the rows will grow over time as the report information grows. One day there could be thousands of rows. I've tried several samples codes but haven't found anything to work. Hopefully someone can help. I'd really appreciate it.

Thanks,

Mark
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try this.

Code:
Sub DelRow()
Application.ScreenUpdating = False
Dim LR As Long: LR = Range("F" & Rows.Count).End(xlUp).Row
Dim dt As Date: dt = #12/7/2018#


For i = LR To 1 Step -1 'change 1 to reflect where your data begins
    If Cells(i, 6).Value < dt Then Rows(i).EntireRow.Delete
Next i
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Thank you very much for the reply. I really appreciate it.

I got the following error message:
Run-time error '1004':
Application-defined or object-defined error

The debug highlighted the following: If Cells(i, 6).Value < dt Then
 
Upvote 0
My apologies. The script is working fine. Thank you very much. This was a huge help. I really appreciate.

Sincerely,

Mark
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
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