Need VBA that deletes content of cell based on adjacent cell's date being older than 21 days

MJ72

Board Regular
Joined
Aug 17, 2021
Messages
64
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Let's say I have 3 columns; "Date", "Contact#", "Notes". Because of privacy restrictions, the information in the cells of the "Contact #" column need to be deleted once their corresponding entry date in the "Date" column reaches 20 days past it's date. I do not, however, need the "Date" column, nor the "Notes" column to be deleted as well. Date = ("G6:G40") and Contact = ("H6:H40"). I know I need an IF statement and Range but can't seem to get it to delete just the cells in "H" that correspond with dates in "G" that are older than 20 days. I'm newish to Excel VBA and have run myself ragged trying to find the solution or combo of solutions online, so... here I am.

I humbly beg for your help :)
DateContactNotes
2021-08-02​
Xset for delete
2021-08-01​
X
2021-07-31​
X
2021-07-30​
X
.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try this on a test copy of your worksheet. Note that if your DateRange is going to change in size or location this is not going to work.

VBA Code:
Sub DeleteOld()
    Dim DateRange As Range, d
    Const AgeCriterion = 21
    Set DateRange = ActiveSheet.Range("G6:G40")
    For Each d In DateRange
        If Now() - d.Value > AgeCriterion Then
            d.Offset(0, 1).ClearContents
        End If
    Next
End Sub
 
Upvote 0
Solution
Try this on a test copy of your worksheet. Note that if your DateRange is going to change in size or location this is not going to work.

VBA Code:
Sub DeleteOld()
    Dim DateRange As Range, d
    Const AgeCriterion = 21
    Set DateRange = ActiveSheet.Range("G6:G40")
    For Each d In DateRange
        If Now() - d.Value > AgeCriterion Then
            d.Offset(0, 1).ClearContents
        End If
    Next
End Sub
Thank you JG, it's almost perfect! No, the date range won't be changing as it will always be 21 days from the date entered and the chance of having more than 34 entries is slim to none. The next query is how to have the macro run every time the excel file is opened?
 
Upvote 0
Put this in the ThisWorkbook Module (access by double clicking ThisWorkbook in the list of Microsoft Excel Objects in the VBA projects panel):

VBA Code:
Private Sub Workbook_Open()
    Sheets("Sheet1").Activate
    DeleteOld
End Sub

Change "Sheet1" to the correct name of your sheet.
 
Upvote 0
Put this in the ThisWorkbook Module (access by double clicking ThisWorkbook in the list of Microsoft Excel Objects in the VBA projects panel):

VBA Code:
Private Sub Workbook_Open()
    Sheets("Sheet1").Activate
    DeleteOld
End Sub

Change "Sheet1" to the correct name of your sheet.
Awesome, JG! Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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