Delete date in cell if date is older than 5 days and adjacent cell is blank (vba)

LilacCat

New Member
Joined
Aug 5, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello, can anyone help me with this please - the macro is deleting all dates older than 5 days even if the adjacent cell is not empty:

VBA Code:
Sub DeleteOldDates()

Dim RFA As Range
Dim RFARng As Range
Dim CurrentDate
Dim tblTracker As ListObject
Dim lrow As Long

Set tblTracker = shTracker.ListObjects("tblTracker")
lrow = tblTracker.Range.Rows(tblTracker.Range.Rows.Count).Row

Set RFARng = Range("Z20:Z" & lrow)
CurrentDate = Date

Application.ScreenUpdating = False

On Error Resume Next

For Each RFA In RFARng
    If RFA.Offset(0, 1) = "" And WorksheetFunction.WorkDay(RFA.Value, 5, Range("Holidays")) < CurrentDate Then
        RFA.ClearContents
    End If

Next RFA

Application.EventsEnable = True
Application.ScreenUpdating = True
    
End Sub

I have also tried if ISBLANK(RFA.Offset(0, 1)) but that didn't work either :confused:

tyvmia
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
You need to step through the code and check your variable (or range) values and/or data types. If you are referring to this line as being the one that deletes
If RFA.Offset(0, 1) = "" And WorksheetFunction.WorkDay(RFA.Value, 5, Range("Holidays")) < CurrentDate Then
then both things must be True even if you think they are not.
One way might be to temporarily comment out the delete line and debug.print the components and check the output. Perhaps like
VBA Code:
Debug.Print RFA.Offset(0,1) & " Function Value: " &  WorksheetFunction.WorkDay(RFA.Value, 5, Range("Holidays")) & " Current_Date: " & CurrentDate
 
Upvote 0
Solution
You need to step through the code and check your variable (or range) values and/or data types. If you are referring to this line as being the one that deletes

then both things must be True even if you think they are not.
One way might be to temporarily comment out the delete line and debug.print the components and check the output. Perhaps like
VBA Code:
Debug.Print RFA.Offset(0,1) & " Function Value: " &  WorksheetFunction.WorkDay(RFA.Value, 5, Range("Holidays")) & " Current_Date: " & CurrentDate
Thank you
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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