Computing.Net > Forums > Office Software > Clearing cells after time

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Clearing cells after time

Reply to Message Icon

Name: mherber2
Date: October 13, 2007 at 09:58:00 Pacific
OS: XP
CPU/Ram: UNKNOWN
Product: UNKNOWN
Comment:

What im looking to do is clear a cell 2 weeks after it is filled but cell a1 should never be cleared...please assisst




Sponsored Link
Ads by Google

Response Number 1
Name: Coldpaws
Date: October 16, 2007 at 03:52:58 Pacific
Reply:

You've asked a tricky one . What you are asking is for excel to have access to dynamic stored information outside the application .Excel then needs to reference this information when the file is opened, compare it with todays date and work out if 2 weeks have elapsed .
The only way I have found that works for this is to use the Windows Registry as the storage location. The comments in the code explain this.
Create a workbook called Celltime.xls and paste in the following code to the worksheet Visual Basic pane. I have chosed cell A4 as the one to delete .
To test it you need to be comfortable opening and editing the registry entry which holds the stored date so you can force it to run the date expiry section of code.
Have a play with the sheet and try overwriting A4 to see what happens. You'll probably need to customise a bit

As for cell A1 - this code doesn't touch it

I've added some messagebox functionality to try and prevent the user doing something unintended.

Paste this code below the line.Good Luck
---------------

Private Sub Worksheet_Change(ByVal Target As Range) 'this section lets the code check for date expiry after any worksheet change

'This macro detects when a specific cell has an entry made in it,A4 in this case, and logs the date and time
'in the registry. It then checks the cell every time the sheet is changed and
' if the current date is more than the specified time from the date the cell was entered
' it clears the cell. It also warns the user if A4 is overwritten before the time has expired

Dim CNumber As Date
Dim nDefault As Date
Dim AgeSetting As Long

' Set the location of the registry entry to hold the sequential number outside Excel
'use Start--> run--> regedit to open the registry
'registry location is 'Software-->VB and VBA Program Settings-->Excel-->"Celltime"-->"Celltime_key"
'"CellTime" is the sheet name used in this example it needs to be your sheet name.
'"CellTime_Key" is just a registry entry name i have selected - it can be anything you want for your application
Const sAPPLICATION As String = "Excel"
Const sSECTION As String = "CellTime"
Const sKEY As String = "CellTime_key"

AgeSetting = 14 ' lets the user set how long the entry in cell A4 can exist before being deleted
' in this case 14 days (2 Weeks)

'sets the test for the cell as the current date and time minus 14 days and formats it
test = Now - AgeSetting
test = Format(test, "General Date")
nDefault = Now

' First check the target cell A4 to see if it has an entry, if not, set the registry date as 'now'
' as the default setting and end the routine
Range("A4").Select
If IsEmpty(Selection) Then
SaveSetting sAPPLICATION, sSECTION, sKEY, nDefault
GoTo finish
End If


CNumber = GetSetting(sAPPLICATION, sSECTION, sKEY) 'retrieve the date the cell was last changed
'from the registry

If test <= CNumber Then ' if the test value is less than the current entry in the registry the cell value
'was changed less than 14 days ago and the deletion section is not run.
'check if the user is changing the target cell and warn them of the consequences
'if not, end the subroutine.

If Target = Range("A4") And Target <> "" Then
response = MsgBox("You are changing Cell A4. Click 'Yes' to reset the timer to delete it in the set number days from now , Click 'No' to keep the existing timing ", vbYesNo)
If response = vbNo Then

GoTo finish
Else
'If the use does want to change cell A4 , the new value of the timer is stored in registry.
SaveSetting sAPPLICATION, sSECTION, sKEY, nDefault
GoTo finish
End If
End If

Else
Response1 = MsgBox("The entry in Cell A4 has expired and will be deleted, 'Cancel' will not delete ", vbOKCancel)
If Response1 = vbOK And Range("A4") <> "" Then
Range("A4") = ""
SaveSetting sAPPLICATION, sSECTION, sKEY, nDefault 'resets the registry entry to todays date and time
GoTo finish
Else
GoTo finish
End If
End If

finish:
Range("A5").Select 'just to move the active cell off the important one, A4 in my example
End Sub


If you're not totally confused , you don't fully understand the question.


0
Reply to Message Icon

Related Posts

See More


doing the unheard of. Outlook2007 | Creating fo...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Office Software Forum Home


Sponsored links

Ads by Google


Results for: Clearing cells after time

Lock empty cell after first edit www.computing.net/answers/office/lock-empty-cell-after-first-edit/8334.html

Lock cell after first edit on shared workbook www.computing.net/answers/office/lock-cell-after-first-edit-on-shared-workbook/8987.html

Can't clear cells in Excel www.computing.net/answers/office/cant-clear-cells-in-excel/2221.html