VBA/MACRO to delete rows based on date

ldashev

New Member
Joined
Mar 24, 2016
Messages
49
I'd like to create a macro to delete rows based on a date (let's say all dates prior to 1/1/2016) from a file. The date field is column B. Could someone please help put together the code? Below is a sample data set


Col1
Col2
Col3
Col4
1
2-Jan-14
ABC
782350
2
2-Jan-14
ABC
782230
3
9-Jan-15
ABC
792270
4
9-Jan-15
ABC
791539
5
2-Jan-15
ABC
782349
6
2-Jan-14
BCD
782230
7
9-Jan-15
BCD
792270
8
9-Jan-15
BCD
791539
9
2-Jan-15
XYZ
782349
10
9-Jan-15
XYZ
792306
11
9-Jan-15
XYZ
791550
12
2-Jan-15
XYZ
783079
13
9-Jan-15
XYZ
791648
14
9-Jan-15
XYZ
792286
15
2-Jan-15
XYZ
782994
16
1-Mar-16
XYZ
792273
17
1-Mar-16
XYZ
791657
18
1-Mar-16
XYZ
791671
19
1-Mar-16
XYZ
782208
20
1-Mar-16
XYZ
792262
21
1-Mar-16
XYZ
790996
22
1-Mar-16
XYZ
793205

<tbody>
</tbody>
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Is your data always going to be sorted chronologically?
If not, is it all right to have our VBA code re-order the data in this way?

Where is this "date" coming from?
Is it user input, or coming from some cell?
 
Upvote 0
try this
Code:
Sub DeleteFromDate()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim LR As Long
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
DateR = Application.InputBox("Enter based on date to delete", TitleMsg, FormatDateTime(Date, vbShortDate), Type:=1)
Cells.AutoFilter Field:=2, Criteria1:=">=" & DateR
ALR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
If ALR > 2 Then
    Range("A2:A" & LR).SpecialCells(xlCellTypeVisible).Select
    Range("A2:A" & LR).Delete
    Range("A1").Activate
End If
Cells.AutoFilter
MsgBox "Finished deleting rows"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0
It works...just needed to change the > to <. Thank you

try this
Code:
Sub DeleteFromDate()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim LR As Long
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
DateR = Application.InputBox("Enter based on date to delete", TitleMsg, FormatDateTime(Date, vbShortDate), Type:=1)
Cells.AutoFilter Field:=2, Criteria1:=">=" & DateR
ALR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
If ALR > 2 Then
    Range("A2:A" & LR).SpecialCells(xlCellTypeVisible).Select
    Range("A2:A" & LR).Delete
    Range("A1").Activate
End If
Cells.AutoFilter
MsgBox "Finished deleting rows"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Hi, similar to the issue above I have pasted set of data (this is only a sample) from which I require the user to be able to select via a message box a date range( col G).Rows outside of this range deleted. This should be look at the last row with data in the worksheet and loop from bottom to top. The headers are in row 2.

Any help would be appreciated.

SiteProject CodeStatusClient NameProject NamePJMActiveBudget Date
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE20-Jan-2015
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE10-Feb-2017
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE28-Feb-2017
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE28-Feb-2017
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE10-Jan-2017
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE28-Feb-2017
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE31-Jul-2016
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE20-Jan-2015
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE31-Jul-2016
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE31-Jan-2017
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE28-Feb-2017
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE10-Feb-2016
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE31-Jul-2016
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE31-Jul-2016
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE28-Feb-2017
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE28-Feb-2017
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE31-Jul-2016
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE20-Oct-2016
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE31-Jul-2016
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE10-Jan-2017
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE28-Feb-2017
xxxxxxxxxxxxIn ProgressxxxxxxxxxxxxxxxTRUE31-Jan-2017

<colgroup><col span="2"><col><col><col><col><col><col></colgroup><tbody>
</tbody>




Thanks
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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