Remove cell contents based on date - VBA

JedZedOne

New Member
Joined
Sep 11, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

Basically, I have a list of names and reasons for people being on a list. Plus the date their names were added to the list.

What I'm wanting is for the line with their info to be removed after 7 days when we open the spreadsheet.

Referring to the image, lets take row 2-4. Their names were added >= 7 days ago.
So A2:C4 should be automatically removed cleared when we open the spreadsheet.

Column D is automatically generated based on the date in Column C. So if you clear C it will clear D.

My knowledge of VBA is extremely limited. But I've tried to piece a few things together. Which isn't quite working the way I want it too. See below.

Sub removedate()
Dim rw As Long
With ActiveSheet
On Error Resume Next
rw = Application.WorksheetFunction.Match(CDbl(Date), .Range("D:D").Value2, 0)
On Error GoTo 0
If rw = 0 Then
rw = Application.WorksheetFunction.Match(CDbl(Date), .Range("D:D").Value2, 1)
End If
.Range("A1:C" & rw).ClearContents
End With

End Sub

Hope this makes sense. Any assistance is always appreciated.
 

Attachments

  • 1.PNG
    1.PNG
    22.3 KB · Views: 16

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
the line with their info to be removed after 7 days when we open the spreadsheet.
For this you'll need to use Workbook_Open event code. It is important to put the code below in the correct place. In the VBA Editor (I assume you know how to get there given the code you've already produced) double-click on ThisWorkbook in the project explorer window (top left of the screen). When a blank window opens on the right of the screen, copy the code below into it, save the file as macro-enabled, then reopen it. Just change the sheet name in the code to whatever your sheet name is first.
VBA Code:
Private Sub Workbook_Open()
    Application.EnableEvents = False
    Dim ws As Worksheet, LRow As Long, i As Long
    Set ws = Worksheets("Sheet1")               '<-- *** Change to actual sheet name ***
    LRow = ws.Cells(Rows.Count, "C").End(xlUp).Row
    For i = LRow To 2 Step -1
        If ws.Cells(i, 3) <= (Date - 7) Then ws.Rows(i).EntireRow.Delete
    Next i
    Application.EnableEvents = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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