Conditional Formatting with an image

Victor Lopez 41 Reputation points
2022-06-03T14:01:17.917+00:00

Hello,

I wanted to add a conditional formatting for expiration dates. Currently I am using :

=EDATE(TODAY(),1) =EDATE(TODAY(),2) =EDATE(TODAY(),3) for 1 month out, 2 months out and 3 months out with Red Orange Green color formatting.

This is working fine, but I wanted to use little traffic lights instead of the color formatting. I managed to find a VBA code that brings up the traffic lights, I am using the following:

Select Case [c14]
Case 1: Shapes("Green1").Visible = msoTrue
Case 2: Shapes("Yellow1").Visible = msoTrue
Case 3: Shapes("Rojo1").Visible = msoTrue
End Select
Select Case [b5]
Case 1: Shapes("Green2").Visible = msoTrue
Case 2: Shapes("Yellow2").Visible = msoTrue
Case 3: Shapes("Rojo2").Visible = msoTrue
End Select

'etc. for all 9 cells

End Sub

Sub HideSignals()
Shapes("Green1").Visible = msoFalse
Shapes("Yellow1").Visible = msoFalse
Shapes("Rojo1").Visible = msoFalse

Shapes("Green2").Visible = msoFalse
Shapes("Yellow2").Visible = msoFalse
Shapes("Rojo2").Visible = msoFalse

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, [c14,B5]) Is Nothing Then
'this does not calculate the worksheet. It just calls the Calculate event.
Worksheet_Calculate
End If
End Sub

The problem with this is, it only works if the value in the cell is 1,2, or 3. I want to make it so that it also uses date values "Today" and then the 30 days, 60 days and 90 days format. I know that the 1,2,3 comes from the value placed after the "Case" (Case #:) but i want to know if I can make that a date and how would I modify it.

Microsoft 365 and Office | Development | Other
Developer technologies | Visual Basic for Applications
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,326 Reputation points
    2022-06-03T14:40:37.647+00:00

    To get the # of months between 2 dates you can use this: =IF(TODAY() > $A2, -DATEDIF($A2, TODAY(),"m"), DATEDIF(TODAY(), $A2,"m")) where $A2 is just a cell reference. This would return the # of months between the given cell's date and today. DATEDIF doesn't like negative values so the IF is required. Given this formula you can then select the correct icon set using conditional formatting.

    If you want to continue doing this in a script then replace the simple select case statement with select case <formula>. You then get back the # of months and can use the simple case statement.

    Select Case IF(TODAY() > c14, -DATEDIF(c14, TODAY(),"m"), DATEDIF(TODAY(), c14,"m"))
    Case 1: Shapes("Green1").Visible = msoTrue
    Case 2: Shapes("Yellow1").Visible = msoTrue
    Case 3: Shapes("Rojo1").Visible = msoTrue
    End Select
    

    If you have that value already calculated in your spreadsheet somewhere then you can use the conditional formatting UI to do the same thing. But if you don't then it becomes harder because icon sets and custom formulas don't work together so you end up having to use multiple rules.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.