View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
Fleone Fleone is offline
external usenet poster
 
Posts: 50
Default Using VB to create "conditional formatting"

Ok, I just got back on the forum here, and saw your response Patrick, I will
try this and see what it does.
In the meantime, I found this buried in another workbook that I have been
working with and made some tweaks and it seems to be working. I have a
question about it though.

Private Sub Worksheet_change(ByVal Target As Range)
If Target.Cells.Count 1 Then Exit Sub
If Intersect(Target, Me.Range("C4:U13")) Is Nothing _
Then Exit Sub
On Error GoTo CleanUp
Application.EnableEvents = False
With Target
Select Case .Value
Case "Lunch": .Interior.ColorIndex = 6
Case "Off": .Interior.ColorIndex = 0
Case "Vacation": .Interior.ColorIndex = 34
Case "Call Off": .Interior.ColorIndex = 44
Case "Holiday": .Interior.ColorIndex = 43
Case "Meeting": .Interior.ColorIndex = 35
Case "Project": .Interior.ColorIndex = 36
Case "Training": .Interior.ColorIndex = 37
End Select
End With
CleanUp:
Application.EnableEvents = True

End Sub

This is working GREAT for changing any of the cell colors in any worksheet
that I add this code to, so as a method for defeating the conditional
formatting limitation, it is doing exactly what I would expect.
The next step in this would be to have the ability to affect a change to the
font color in each of the cells according to their content. I have already
tried making another entry in the worksheet identical to the one above, but
changing Interior.ColorIndex to Font.ColorIndex. That doesn't work, I recieve
an error on using an "ambigous" <sp? name. When I try to duplicate one of
the lines, and making the same change, it doesn't work either.
It has become a real head scratcher <G

Any insight you can provide would be great!
I will try the code that you have already provided.
Thanks again!


"Patrick Molloy" wrote:

Using VBA.
Add a standard code module and paste the following:

Option Explicit

Enum eColors
Yellow = 10092543
Green = 13434828
Blue = 16777164
Grey = 12632256
Orange = 10079487
End Enum

Sub MyConditionalFormatting(Target As Range)

Dim cell As Range
Dim clr As Long

For Each cell In Target


Select Case UCase(Trim(cell.Value))
Case "LUNCH": clr = eColors.Yellow
Case "OFF": clr = eColors.Blue
Case "HOLIDAY": clr = eColors.Grey
Case "COURSE": clr = eColors.Orange
Case Else: clr = eColors.Green
End Select

cell.Interior.Color = clr

Next

End Sub
Sub test()
MyConditionalFormatting Range("MyData")
End Sub

To test
On a sheet, select a range, name it "MyData" type some values into some
cells in th erange randomly, include: Holiday, Lunch, Off, Course

run the Test procedure. This passes the range "MyData" to the procedure that
then does the formatting.

This example allows four colors, but you can see that its quite easy to
adapt to use more colors.

HTH
Patrick Molloy
Microaoft Excel MVP



"Fleone" wrote:

I would like to have cells in a workbook formatted depending on their
contents, not their formulas.
For example: If a cell contains the word "Lunch" I want the cell to be
formatted in Yellow, all through the workbook.
Transversely, if a cell does not contain the word "Off", I would like it to
be Blue, all through the workbook.
I have little practical Visual Basic experience and most of what I have
already picked up has been from reverse engineering other scripts.
Would appreciate any assistance.
Thanks.