highlight cell according to date difference

Lux Aeterna

Board Regular
Joined
Aug 27, 2015
Messages
205
Office Version
  1. 2019
Platform
  1. Windows
Hello everyone!

I' like a cell to be automatically hightlighted when the date in that cell is 15 days older than the day I type in that cell!

For example, today is October 21st, and in cell F5 I am typing the date October 1st. I'd like that cell to be highlited (I'm going to use it for the cells F5 to F10000), because the difference is more than 15 days.
(I work in a lab and it is the day of the sample receipt (column F) and the day we process the sample)

I thought of doing it using conditional formating and today function, but this means that gradually all my dates will be more than 15 days old. I saw there is an option to stop automatic calculations, but I can't use that, because the whole sheet is full of functions that I need to be updated automatically.

I also thought of adding an extra column with the day I type in the data, which would probably work just fine, but I'm trying to minimise the thingsI need to insert in the sheet.

Thank you in advance
 
Thanks, it works like a charm! I also found a workaround. I allowed format to locked cells.
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try this slight variation:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
   
    Set rng = Intersect(Target, Range("F5:F10000"))
   
    If rng Is Nothing Then Exit Sub
   
    For Each cell In rng
        If (cell <> "") And (Date - cell > 15) Then
            cell.Interior.Color = 65535
        Else
            cell.Interior.Pattern = xlNone
        End If
    Next cell

End Sub
Hello again!

Your code has been working perfectly until today.

I am getting this message.

1658817143513.png

Any idea why?

I've mede some changes to the sheet, but non of them is related to the F column.
 
Upvote 0
What exactly is the error message you are getting?
Do you have any merged or protected cells on the sheet?
 
Upvote 0
What exactly is the error message you are getting?
Do you have any merged or protected cells on the sheet?
1658850383321.png


It seems the problem was password protection. When I locked the sheet I di not allow cell formatting. I didn't expect that to be the problem, because the specific column is not locked :unsure:
 
Upvote 0
View attachment 70144

It seems the problem was password protection. When I locked the sheet I di not allow cell formatting. I didn't expect that to be the problem, because the specific column is not locked :unsure:
Yes, when something works for a long time, then suddenly stops working, it is always best to consider any recent changes you made.

The easiest way to handle this (assuming you want to keep the protection) is to add a line of code unprotecting the sheet. I would do this right under this line of code:
VBA Code:
For Each cell in rng
    Worksheets("SheetName").Unprotect "Password"
where "SheetName" is the name of the sheet and "Password" is your actual password.

Then, you can re-protect again after the last change. I would put this just above the following line of code:
VBA Code:
    Worksheets("SheetName").Protect "Password"    
Next cell

See here for more details on protecting/unprotecting sheets via VBA code:
 
Upvote 0
Hey, again!

I'd like a small adjustment, if possible.

The code works perfectly fine. But if I mistype the date (e.g. if I accidentally use double slash or add extra numbers to the date) I get a debug error.

I'd like an adjustment so that the macro clears the cell if the value I enter is not in the usual format of a date.

Thank you in advance!

Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range
Dim cell As Range

Set rng = Intersect(Target, Range("F5:F10033"))

If rng Is Nothing Then Exit Sub

For Each cell In rng
If (cell <> "") And (Date - cell > 30) Then
cell.Interior.Color = 65535
Else
cell.Interior.Pattern = xlNone
End If
Next cell

End Sub
 
Last edited:
Upvote 0
Hey, again!

I'd like a small adjustment, if possible.

The code works perfectly fine. But if I mistype the date (e.g. if I accidentally use double slash or add extra numbers to the date) I get a debug error.

I'd like an adjustment so that the macro clears the cell if the value I enter is not in the usual format of a date.

Thank you in advance!
Note the section in red that I added:
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range

    Set rng = Intersect(Target, Range("F5:F10033"))

    If rng Is Nothing Then Exit Sub

    For Each cell In rng
        If (cell <> "") And Not IsNumeric(cell) Then
            Application.EnableEvents = False
            cell.ClearContents
            Application.EnableEvents = True
        End If
        If (cell <> "") And (Date - cell > 30) Then
            cell.Interior.Color = 65535
        Else
            cell.Interior.Pattern = xlNone
        End If
    Next cell

End Sub
 
Upvote 0
Note the section in red that I added:
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range

    Set rng = Intersect(Target, Range("F5:F10033"))

    If rng Is Nothing Then Exit Sub

    For Each cell In rng
        If (cell <> "") And Not IsNumeric(cell) Then
            Application.EnableEvents = False
            cell.ClearContents
            Application.EnableEvents = True
        End If
        If (cell <> "") And (Date - cell > 30) Then
            cell.Interior.Color = 65535
        Else
            cell.Interior.Pattern = xlNone
        End If
    Next cell

End Sub
Thanks a lot! I'll check it out tomorrow!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
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