Color cell when date is more than 40 days

Sha

New Member
Joined
Oct 6, 2021
Messages
30
Office Version
  1. 2013
Platform
  1. Windows
Hi

this is the code I have thus far.


Sub HighlightCells()

Dim dtrg As Range 'Date in Col J
Dim dtCell As Range 'Date Cell
Set dtrg = Range("J1:J3000") ' Date Range

For Each dtCell In dtrg.Cells
If dtCell.Value > dtCell.value + 40 Then
dtCell.Interior.ColorIndex = 3
End If
Next dtCell


End Sub
 
Last edited by a moderator:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You are testing the cell against itself and so it can never be greater than itself +40.
If you are comparing it to todays date then it is
VBA Code:
If dtCell.Value > Date + 40 Then
Or older than 40 days
VBA Code:
If dtCell.Value < Date - 40 Then
 
Last edited:
Upvote 0
Hi,

The cell is still not highlighted... do need to set the col range to date like what I have below, cause this does not work either.
SQL:

VBA Code:
Sub HighlightCells()

Dim dtrg As Date: Set dtrg = Range("J1:J3000")
Dim dtCell As Range

For Each dtCell In dtrg.Cells
If dtCell.Value < Date - 40 Then
dtCell.Interior.ColorIndex = 3

End If
Next dtCell

End Sub
 
Upvote 0
Please see the edited code in my post if you are highlighting dates older than 40 days
 
Upvote 0
Is it working for you now? I assume that your "typo" was declaring dtrg as Date rather than Range?
 
Upvote 0
What are you trying to highlight exactly? Dates older than 40 days ago or more recent than 40 days ago?
 
Upvote 0
HIghlighting cells which are older than 40 days.

ie if col j has a date (01/10/2020) it should be hightlighted if today was 10/11/2020. however, i do not wish to use the today() function
 
Upvote 0
The code below does that for me
VBA Code:
Sub HighlightCells()

    Dim dtrg As Range: Set dtrg = Range("J1:J3000")
    Dim dtCell As Range

    For Each dtCell In dtrg.Cells
        If dtCell.Value <> "" And dtCell.Value < Date - 40 Then _
           dtCell.Interior.ColorIndex = 3
    Next dtCell

End Sub

If it doesn't for you...
a) where have you placed the code
b) how are you running it
c) is the same sheet active
d) if you place the formula
Excel Formula:
=ISNUMBER(J1)
in the first row of an empty column and drag down do you get all TRUE's or do you get some that are FALSE
 
Upvote 0
Solution

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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