Private Sub Worksheet_Activate()
If Date > Range("D1").Value + 365 Then Range("D1").ClearContents
End Sub
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet that contains the date and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. This macro will check the date in D1 every time the sheet is activated and clear the date if it is a year or more old.
VBA Code:Private Sub Worksheet_Activate() If Date > Range("D1").Value + 365 Then Range("D1").ClearContents End Sub
How many sheets are in your workbook?So, I tried the exact VBA code as described for D1 and it worked...once. I put in the code, save it, close the workbook, open it up and the date is gone. If I put the date back in and go through that process again, the date remains. Am I missing something?
How many sheets are in your workbook?
What is the name of the sheet that you want this to apply to?
What value do you have in cell D1?
Private Sub Workbook_Open()
Dim cell As Range
Dim cl As Long
Dim rw As Long
Application.ScreenUpdating = False
' Loop through columns D to AA
For cl = 4 To 27
' Loop through odd number rows from to 3 to 59
For rw = 3 To 59 Step 2
' Check dates in cells to see they are more than a year old
If (Cells(rw, cl) > 0) And (Date > (Cells(rw, cl) + 365)) Then
' Clear date from cell
Cells(rw, cl).ClearContents
End If
Next rw
Next cl
Application.ScreenUpdating = True
End Sub
OK, because there is only one sheet, I think it is best to use a Workbook_Open event that runs when you first open the workbook.
Place the following code in the "ThisWorkbook" module, and the code will run automatically when opening the file:
VBA Code:Private Sub Workbook_Open() Dim cell As Range Dim cl As Long Dim rw As Long Application.ScreenUpdating = False ' Loop through columns D to AA For cl = 4 To 27 ' Loop through odd number rows from to 3 to 59 For rw = 3 To 59 Step 2 ' Check dates in cells to see they are more than a year old If (Cells(rw, cl) > 0) And (Date > (Cells(rw, cl) + 365)) Then ' Clear date from cell Cells(rw, cl).ClearContents End If Next rw Next cl Application.ScreenUpdating = True End Sub