Link to home
Start Free TrialLog in
Avatar of KRS12
KRS12

asked on

Excel 2010 - Delete Row based on date

Delete Row based on date formula required.

I have a  column that I need to identify the dates older than...........then delete the rows... I have searched and nothing I try is working.
Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

You cannot delete rows with a formula. You would need VBA to do that.

Post an example workbook if you want code to do this.
Avatar of KRS12
KRS12

ASKER

Start Date      Start Time      End Date      End Time      
9/7/2012      9:00      9/7/2012      10:00      1      
9/7/2012      10:00      9/7/2012      11:00      1      
11/1/2013      10:00      11/1/2012      10:45      0.75
Please find attached sample, type your date in B1 and delete old data.
Delete-Rows-as-per-Date.xlsm
Please try below code, if you want to delete rows just on Start Date, assuming your Start date is in Column A.
Private Sub DeleteOldData_Click()
Dim Ws As Worksheet
Dim LR As Long
Dim sDate As Date, fDate As Date
Application.ScreenUpdating = False
Set Ws = Worksheets("Sheet1")
LR = Ws.Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    sDate = Ws.Cells(i, 1).Value
    fDate = Ws.Cells(1, 5).Value
    If sDate <= fDate Then
        Ws.Cells(i, 1).EntireRow.Delete
    End If
Next i
Application.ScreenUpdating = True
End Sub

Open in new window

If you want to delete rows between Start Date & End Date, then try below code:
Private Sub DeleteOldData_Click()
Dim Ws As Worksheet
Dim LR As Long
Dim sDate As Date, eDate As Date, fDate As Date
Application.ScreenUpdating = False
Set Ws = Worksheets("Sheet1")
LR = Ws.Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    sDate = Ws.Cells(i, 1).Value
    eDate = Ws.Cells(i, 3).Value
    fDate = Ws.Cells(1, 5).Value
    If sDate <= fDate And eDate <= fDate Then
        Ws.Cells(i, 1).EntireRow.Delete
    End If
Next i
Application.ScreenUpdating = True
End Sub

Open in new window

Please see attached for your reference.
Delete-Rows-as-per-Date_V2.xlsm
The most efficient way to delete date is to automate AutoFilter.

This code will ask the user to input the date. All dates prior to that date will be deleted. It assumes that the dates are in Column A

Option Explicit

Sub deleteFilteredDates()
    Dim rDelete    As Range
    Dim lCalc  As Long
    Dim dt
     
    With Application
        lCalc = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        
 On Error GoTo exit_proc
     
     'Obtain the value that you want to delete
    dt = Application.InputBox("Enter date  for deletion")
    If dt = 0 Then Exit Sub 'user cancelled
   
     'Sheet with the data, change the name
    With Sheet1
    If Not .AutoFilterMode Then .Range("A1").AutoFilter
       
         
         'Apply the filter, this range of data starts in A1
        .Cells(1, 1).CurrentRegion.AutoFilter Field:=1, Criteria1:="<" & CLng(CDate(dt)), Operator:=xlAnd
         
        With .AutoFilter.Range
            On Error Resume Next
            Set rDelete = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
            .SpecialCells(xlCellTypeVisible)
            If Not rDelete Is Nothing Then rDelete.EntireRow.Delete
        End With
         'Remove the AutoFilter
        .AutoFilterMode = False
    End With
     
exit_proc:
        .ScreenUpdating = True
        .Calculation = lCalc
    End With
     
End Sub

Open in new window


Post back if you want help placing the code in your project.
AutoFilter-with-Dates.xlsm
Avatar of KRS12

ASKER

Shums, thank you.  I want to use the solution:  If you want to delete rows between Start Date & End Date, then try below code:
What lines do I modify?  I need to have a start date of 9/7/2012 and end date of 2/1/2017
ASKER CERTIFIED SOLUTION
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KRS12

ASKER

Excellent thank you so much, it worxs excellent :) :)