Forum Discussion
LesKing
Apr 06, 2023Brass Contributor
Conditional Formatting
The Macro below works perfectly when column “L” is filled with numbers BUT it will actually be filled with dates, and I cannot work out how to make it work when column L has dates in it. Can anyone help please?
'Sub Critical Dates()
Dim rg As Range
Dim iset As IconSetCondition
Set rg = Range("L1", Range("L1").End(xlDown))
'Delete previous formatting conditions
Sheets("CriticalDates").Select
rg.FormatConditions.Delete
‘Choose Traffic Lights icon set
Sheets("CriticalDates").Select
Columns("L:L").Select
Selection.FormatConditions.AddIconSetCondition
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1)
.ReverseOrder = False
.ShowIconOnly = False
.IconSet = ActiveWorkbook.IconSets(xl3TrafficLights1)
End With
‘Set AMBER Traffic Light Condition
With Selection.FormatConditions(1).IconCriteria(2) 'IconCriteria(2) is AMBER traffic light
.Type = xlConditionValueNumber 'sets xlConditionValue to a number
.Operator = 7 'xlGreaterEqual
.Value = 30 'i.e. between this number (30) and 60 '(60 is the GREEN no. below)
End With
‘Set GREEN Traffic Light Condition
With Selection.FormatConditions(1).IconCriteria(3) 'IconCriteria(3) is GREEN traffic light
.Type = xlConditionValueNumber 'sets xlConditionValue to a number
.Operator = 7 'xlGreaterEqual
.Value = 60 'i.e. from 60 up - formula would in effect be <=61
End With
End Sub
I thought the macro would work with DATEDIF function to try to get a number: DATEDIF(TODAY(),L2,”D”) or DATEDIF(NOW(),L2,”D”) in the hope the value would be return a number (i.e. the number of days between L2 and today), but I couldn’t get the right syntax for the macro to work. I also tried using xlConditionFormula instead of xlConditionNumber but couldn’t work out how to do that. Can anyone please help to get the macro to achieve what I want (assuming it is possible). Many thanks. Les King
Try this:
Sub CriticalDates() Dim rng As Range Dim isc As IconSetCondition Set rng = Range("L1", Range("L1").End(xlDown)) rng.FormatConditions.Delete Set isc = rng.FormatConditions.AddIconSetCondition With isc .ReverseOrder = False .ShowIconOnly = False .IconSet = ActiveWorkbook.IconSets(xl3TrafficLights1) With .IconCriteria(2) .Type = xlConditionValueFormula .Value = "=TODAY()+30" .Operator = xlGreaterEqual End With With .IconCriteria(3) .Type = xlConditionValueFormula .Value = "=TODAY()+60" .Operator = xlGreaterEqual End With End With End Sub
Try this:
Sub CriticalDates() Dim rng As Range Dim isc As IconSetCondition Set rng = Range("L1", Range("L1").End(xlDown)) rng.FormatConditions.Delete Set isc = rng.FormatConditions.AddIconSetCondition With isc .ReverseOrder = False .ShowIconOnly = False .IconSet = ActiveWorkbook.IconSets(xl3TrafficLights1) With .IconCriteria(2) .Type = xlConditionValueFormula .Value = "=TODAY()+30" .Operator = xlGreaterEqual End With With .IconCriteria(3) .Type = xlConditionValueFormula .Value = "=TODAY()+60" .Operator = xlGreaterEqual End With End With End Sub
- LesKingBrass Contributor
Hi Hans,
That worked perfectly - and was much more straightforwad than the things I was trying!!
Thank you so much!
Les King