Automatically delete rows after time period using time stamp (have partial code)

RainbowStorm

New Member
Joined
Oct 18, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hiya

I am looking to apply a timestamp, and automatically delete the row when it is 4 years old. I can make it work to automatically delete the row within a 24 time period, but I cannot set larger periods or I am presented with a type mismatch.
This is the code I was using

Private Sub Worksheet_Activate()
Dim stampcell As Range
Dim TLSh As Worksheet
Set TLSh = Worksheets("Deselected Time Log")
Application.ScreenUpdating = False
For Each stampcell In TLSh.Range("A2:X300")
If Now > stampcell.Value2 + TimeValue("23:59:59") Then
Me.Range(stampcell.Address).ClearContents
stampcell.ClearContents
End If
Next stampcell
Application.ScreenUpdating = True
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRng As Range
Dim stampcell As Range
Dim TLSh As Worksheet
Application.EnableEvents = False
Set TLSh = Worksheets("Deselected Time Log") "timestamp sheet
Set myRng = Range("A2:X300")
If Not Intersect(Target, myRng) Is Nothing Then
TLSh.Range(Target.Address).Value2 = Now
TLSh.Range(Target.Address).NumberFormat = "MM/DD/YYYY HH:MM:SS"
End If
Application.EnableEvents = True
End Sub

I identified the issue was quantifying it as time value so attempted to change it to the following:
If Now > stampcell.Value2 + DateAdd("d", 1, stampcell.Value2) Then

This was so I could test it before changing it to 6 years.

Is there anyway for me to do this?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi there,

This will check for 4 years:

VBA Code:
If Now > stampcell.Value2 + 365 * 4  Then

Posting the same question in several forums??
 
Upvote 0
Hi there,

This will check for 4 years:

VBA Code:
If Now > stampcell.Value2 + 365 * 4  Then

Posting the same question in several forums??
Hiya, sorry I wasn't aware the forum's were linked :(
I've reported my own posts to be removed
 
Upvote 0
Hiya, sorry I wasn't aware the forum's were linked :(
I've reported my own posts to be removed
I don't think they're linked. I just happen to have accounts in several Excel forums, I saw thw same question from the same user name.
:)
 
Upvote 0
If the answer given solves your problem, please mark it as an answer.
 
Upvote 0
Using Now for the comparison is taking the time of day into consideration so I would think the number of days would also be important.
365*4 ignores the 29th of February and will always be 1 day short of 4 years and possibly 2 days short when you change to 6 years.

I'd use the DateAdd function and Excel will look after possible leap years.
VBA Code:
    If DateAdd("yyyy", 4, stampcell) < Now Then
 
Upvote 0
Solution
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Automatically delete rows after time period using time stamp (have partial code)
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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