VBA Colour Code Cell if X Days Greater than Today

SiMoore

New Member
Joined
Dec 10, 2019
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hoping for some assistance, in brief I'm trying to apply a conditional format to a column based on a a number of days, the first condition below will highlight the cell if the date is less than today's date which works perfectly. The next condition I would like it to change the colour of the cell to amber only if the date is between today and 90 days and then a final condition would be to colour the cell green if it is beyond 90 days.

I've tried every variation going and searched high-and-low but can't quite figure it out.

Thanks in advance.

'Support End Date (Less Than Today)
With ThisWorkbook.Worksheets("Output").Range("H2:H1000").FormatConditions
.Add Type:=xlExpression, Formula1:="=AND(H2<>"""",H2<TODAY())"
With .Item(.Count)
.Interior.Color = 255
.SetFirstPriority
End With

'Support End Date (Between Today and 90 Days)
With ThisWorkbook.Worksheets("Output").Range("H2:H1000").FormatConditions
.Add Type:=xlExpression, Formula1:="=AND(H2<>"""",H2>TODAY())"
With .Item(.Count)
.Interior.Color = RGB(255, 102, 0)
.SetFirstPriority
End With

'Support End Date (91 Days Onwards)
With ThisWorkbook.Worksheets("Output").Range("H2:H1000").FormatConditions
.Add Type:=xlExpression, Formula1:="=AND(H2<>"""",H2>TODAY())"
With .Item(.Count)
.Interior.Color = RGB(153, 204, 0)
.SetFirstPriority
End With
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Please use code tags when posting code. See underlined portions. Underlining did not seem to survive submittal. BTW why are you doing this in code if you are hardcoding all the ranges? Why not just implement it directly in the sheet?

VBA Code:
    'Support End Date (Less Than Today)
    With ThisWorkbook.Worksheets("Output").Range("H2:H1000").FormatConditions
        .Add Type:=xlExpression, Formula1:="=AND(H2<>"""",H2<TODAY())"
        With .Item(.Count)
            .Interior.Color = 255
            .SetFirstPriority
    End With

   'Support End Date (Between Today and 90 Days)
    With ThisWorkbook.Worksheets("Output").Range("H2:H1000").FormatConditions
        .Add Type:=xlExpression, Formula1:="=AND(H2<>"""",H2>TODAY(),H2<=TODAY()+90)"
        With .Item(.Count)
            .Interior.Color = RGB(255, 102, 0)
            .SetFirstPriority
    End With

  'Support End Date (91 Days Onwards)
    With ThisWorkbook.Worksheets("Output").Range("H2:H1000").FormatConditions
        .Add Type:=xlExpression, Formula1:="=AND(H2<>"""",H2>TODAY()+90)"
        With .Item(.Count)
            .Interior.Color = RGB(153, 204, 0)
            .SetFirstPriority
    End With
 
Upvote 0
That's fantastic thank you so much. Sorry, bit of a novice to all this so just fumbling my way through via Google, message boards and YouTube so as you state above "Why haven't I implemented this directly into the sheet" - the sheet is created on the fly, with the above it's pretty much complete and yes there probably is a better, more refined way to produce the same overall output but it works so mission accomplished.

Thanks again
 
Upvote 0
Sorry one final question - just need to add in one final condition that colour codes based on between 180-365 days. Above provides a colour code for anything beyond 90 days and i've tried the below but this now simply colour codes everything less than 365 days as green so overwrites any previous condition.

'Support End Date (Between 180-365 Days)
With ThisWorkbook.Worksheets("Output").Range("H2:H1000").FormatConditions
.Add Type:=xlExpression, Formula1:="=AND(H2<>"""",H2<TODAY()+365)"
With .Item(.Count)
.Interior.Color = RGB(153, 204, 0)
.SetFirstPriority
End With
 
Upvote 0
Managed to figure it out:

'Support End Date (Within 180-365 Days)
With ThisWorkbook.Worksheets("Output").Range("H2:H1000").FormatConditions
.Add Type:=xlExpression, Formula1:="=AND(H2<>"""",H2>TODAY()+180,H2<=TODAY()+365)"
With .Item(.Count)
.Interior.Color = RGB(153, 204, 0)
.SetFirstPriority
End With
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,961
Latest member
nzskater

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