highlight cell according to date difference

Lux Aeterna

Board Regular
Joined
Aug 27, 2015
Messages
201
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
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
So, do you only want it to check that as you enter the value in the cell (and not done the road, for values already entered in)?
If so, you can use VBA to do that.

Righ-click on the sheet tab name (at the bottom of the screen) that you wish to apply this too, select View Code, and paste this code in the resulting VB Editor window:
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 <> "") Then
            If (Date - cell > 15) Then cell.Interior.Color = 65535
        Else
            cell.Interior.Pattern = xlNone
        End If
    Next cell

End Sub
This should do what you want automatically as you enter data into F5:F10000
 
Upvote 0
So, do you only want it to check that as you enter the value in the cell (and not done the road, for values already entered in)?
If so, you can use VBA to do that.
This should do what you want automatically as you enter data into F5:F10000

Yes, that's exactly what I need, thank you! Is it possible to reset to no highlight when I correct that cell value?
 
Upvote 0
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
 
Upvote 0
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
I am facing a small problem. If I protect the sheet with a password, after I type something in the F column I get a debug message. Any idea what it could be and how to fix it? Thank you in advance.

1642074335527.png
 
Upvote 0
If you click "Debug", which line of code does it highlight?
 
Upvote 0
OK, it sounds like the range is protected, so you are not able to make changes to it.
You will need to add lines to your code to first unprotect the sheet, then make those updates, then re-protect the sheet, i.e.
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:F10000"))
    
    If rng Is Nothing Then Exit Sub

    ActiveSheet.Unprotect "password"
    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
    ActiveSheet.Protect "password"

End Sub
Be sure to replace "password" with your actual password.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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