View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Changing a cell color based on date entry ...

Just to add -
After applying a constant to a cell colour format, Excel matches the colour
to the nearest that exists in the palette, then applies the colorindex
associated with the nearest matching colour. In other words, if the constant
as an RGB value does not exist in the palette the closest according to
Excel's (not very good) colour match algorithm is applied.

All the Enum constants given by Patrick do exist in Excel's Default palette,
and so will be matched exactly (assuming an uncustomized palette).

Some of these, but not the Pink & Brown, could be replaced by vb constants
that already exist, eg vbRed, vbYellow. vbBlue is not the same as Patrick's
Enum Blue, colorindex's 5 & 41 respectively.

I find it's somewhat slower to apply an RGB colour rather than a colorindex,
but unlikely to be noticed in such a routine.

Regards,
Peter T

"Patrick Molloy" wrote in message
...
This may be of some help. Add the following code to a standard code module

Option Explicit
Enum eColor
White = 16777215
Blue = 16737843
Red = 255
Green = 65280
Brown = 13209
yellow = 65535
Pink = 13408767
End Enum
Sub GetAndSetColors()
Dim index As Long
index = 1
Do Until Cells(index, "A") = ""
SetColor Cells(index, "A")
index = index + 1
Loop
End Sub
Private Sub SetColor(target As Range)
Dim clr As Long
Select Case True
Case target.Value = Date
clr = eColor.White
Case target.Value (Date - 7)
clr = eColor.Red
Case target.Value (Date - 14)
clr = eColor.yellow
Case target.Value (Date - 21)
clr = eColor.Green
Case Else
clr = eColor.Pink
End Select
target.Interior.Color = clr
End Sub

In the Worksheet's change event, add a call to the GetAndSetColors

procedu-

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
GetAndSetColors
End If
End Sub


Using ENUM allows you to create your own variable to which you can assign
colors - and this makes your code much mor ereadable. Also, using the

SELECT
CASE again makes your code more manageable ...just add more CASE
staements...and its so much easier to read and debug than shed loads of
IF...ELSEIF statements

HTH


"T. Denford" wrote:

Looking for solution to problem if anyone can help please ...

Column A contains a list of date values (e.g. 11/04/2005).

I'm looking for a way to change the cell color based on the following
criteria ...

If the date falls earlier than today but no earlier than a week before
today then cell color to be red.

If the date falls earlier than today but no earlier than two weeks

before
today then cell color to be yellow.

If the date falls earlier than today but no earlier than three weeks

before
today then cell color to be green.

I have used the sample code as detailed here ...

http://tinyurl.com/3cex5
(kindly advised by Max in microsoft.public.excel) and need to tailor the
code to suit the different date ranges. Can anyone help out please as

I'm
not sure how to build this is to the CASE statements.

Many thanks.

--
T. Denford.