Automatic Delete entries with dates that are less than todays

papare

New Member
Joined
Nov 24, 2013
Messages
26
I am looking for a logic/macro/vba code that will automatically delete certain entries that are older than todays date.

At the moment I collect data using the below code:
Sub upload()

Sheets("Input").Range("C2,C3,C4,C5,C6,C7,C9").Copy
Sheets("Data").Range("C" & Rows.Count).End(xlUp).Offset(1).PasteSpecial _
Paste:=xlPasteValues, Transpose:=True
Sheets("Input").Range("C2:C9").ClearContents

End Sub

The Column C in DATA (Sheet) is the date, Column D is the description that determines that data to be deleted. At the moment I do it manually but would like for it to be automated.
Logic at the moment: IF(LEFT(D2,2)="CS","",I2)
The logic that I was thinking would be better is and embeded in the macro/code: IF(and (LEFT(D2,2)="CS",C2<today),"",I2).

Kindly assist with the code to be embeded with the above macro/vba code.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Just to clarify ... if the first 2 characters in D2 = "CS", then you want cell D2 to be cleared, otherwise you want D2 to be replaced with I2. Is this correct?
 
Upvote 0
Thanks for your reply.
If the first 2 characters in D2 = "CS", then you want cell I2 to be cleared, otherwise do nothing. But this is only for dates Column C, that are less than todays date.
 
Upvote 0
Try:
Code:
Sub ClearCell()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim rng As Range
    For Each rng In Range("C2:C" & LastRow)
        If rng < Date And Left(rng.Offset(0, 1), 2) = "CS" Then
            Cells(rng.Row, "I").ClearContents
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Mums,
tried to change the code

Private Sub Workbook_Open()
Application.ScreenUpdating = False
Dim LastRow As Long
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim rng As Range
For Each rng In Range("C2:C" & LastRow)
If rng < Date And Left(rng.Offset(0, 1), 2) = "CS" Then
EntireRow.Delete
End If
Next rng
Application.ScreenUpdating = True
End Sub

now just deleting the entire row, but its only deleting one row (e.g. I have 2/3 entries with the first 2 letters "CS", on the same date).

Kindly Assist
 
Upvote 0
When deleting entire rows, we have to start at the bottom of your data and work our way up to the top. Try:
Code:
Sub DelRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim x As Long
    For x = LastRow To 2 Step -1
        If Cells(x, "C") < Date And Left(Cells(x, "D"), 2) = "CS" Then
            Rows(x).EntireRow.Delete
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
hi mumps,

on a different query.. using my loading code I want to add a sorting code that will sort the dates. Date (Column C, Sheet (DATA) and range (from A2:A50000). .. Your assistant will be very appreciated... Many Thanks in advance..

Sub upload()

Sheets("Input").Range("C2,C3,C4,C5,C6,C7,C9").Copy
Sheets("Data").Range("C" & Rows.Count).End(xlUp).Offset(1).PasteSpecial _
Paste:=xlPasteValues, Transpose:=True
Sheets("Input").Range("C2:C9").ClearContents

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,343
Messages
6,124,400
Members
449,156
Latest member
LSchleppi

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