Use Case statement to change colour of date

Dennistoun Dark

New Member
Joined
Jun 1, 2016
Messages
28
I am looking to use a Case statement to change the font colour of a date: today's date, a date in the future and a date past.

I have tried the following using Date Today and Now but get an error each time.

'Colour cells based upon value (column J)
Select Case Cell.Offset(0, -2).Value
Case Today
Cell.Offset(0, -2).Interior.FontIndex = 2
Case Is < Today
Cell.Offset(0, -2).Interior.FontIndex = 1
Case Is > Today
Cell.Offset(0, -2).Interior.FontIndex = 4
If Cell.Value = "C" Then Cell.Offset(0, -2).Font.ColorIndex = 1
End Select

The general format of Case is fine because I am using it to change actions in a number of column and rows bed upon txt strings. The column offset from row L is correct as well because I can use Case statements on text strings to change it.

Any help would be appreciated.

Cheers

Frank
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
TODAY is an EXCEL function

In VBA, the equivalent is DATE

and
cell.Offset(0, -2).Font.ColorIndex = 2
 
Last edited:
Upvote 0
I have tried:
'Colour cells based upon value (column J)
Select Case Cell.Offset(0, -2).Value
Case Today
Cell.Offset(0, -2).Interior.ColorIndex = 43
Case Is < Today
Cell.Offset(0, -2).Interior.FontIndex = 1
Case Is > Today
Cell.Offset(0, -2).Interior.FontIndex = 4
If Cell.Value = "C" Then Cell.Offset(0, -2).Font.ColorIndex = 2
End Select

I still get an error
 
Upvote 0
Not sure if your IF statement is in the correct place
- only applies if Case Is > Date


Code:
'Colour cells based upon value (column J)

Select Case cell.Offset(0, -2).Value
    Case Date
        cell.Offset(0, -2).[COLOR=#ff0000]Font.ColorIndex [/COLOR]= 2
    Case Is < Date
        cell.Offset(0, -2).Font.ColorIndex = 1
    Case Is > Date
        cell.Offset(0, -2).Font.ColorIndex = 4
        If cell.Value = "C" Then cell.Offset(0, -2).Font.ColorIndex = 1
End Select

When posting code please click on # icon above post window and ...
[ CODE] paste your code between the tags [ /CODE]
 
Last edited:
Upvote 0
Hi,
try this update to your code


Code:
'Colour cells based upon value (column J)
    With cell.Offset(0, -2)
        If UCase(cell.Value) = "C" Then
            .Font.ColorIndex = 1
        ElseIf IsDate(.Value) Then
            Select Case .Value
            Case Date
                .Font.ColorIndex = 2
            Case Is < Date
                .Font.ColorIndex = 1
            Case Is > Date
                .Font.ColorIndex = 4
            End Select
        End If
    End With

It is assumed that your Range Object variable Cell has been correctly intialized.

Dave
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,206
Members
452,618
Latest member
Tam84

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