Conditional Formatting--Need 4 conditions

G

Guest

I need to add a fourth condition to a column. I have already used 3 which is
apparently the limit that excel will allow. I have a column that contains
the text Red, Green, Yellow or Blue. I have set the conditional formatting
for Red, Green and yellow and everything works fine. I need to add an
additional conditional to turn the cell Blue if it contains the text Blue.
Can anyone help??????
 
G

Gord Dibben

CB

The fourth condition could be the default color for those cells.

i.e. Set the 3 conditions for yellow, green and red and the default "no color"
is used for yellow.

Or dispense with CF and go to sheet event code if you choose.

Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Num As Long
Dim rng As Range
Dim vRngInput As Variant
Set vRngInput = Intersect(Target, Range("A:A"))
If vRngInput Is Nothing Then Exit Sub
On Error GoTo endit
Application.EnableEvents = False
For Each rng In vRngInput
'Determine the color
Select Case rng.Value
Case Is = "Red": Num = 3
Case Is = "Green": Num = 10
Case Is = "Yellow": Num = 6
Case Is = "Blue": Num = 5
End Select
'Apply the color
rng.Interior.ColorIndex = Num
Next rng
endit:
Application.EnableEvents = True
End Sub

This is sheet event code. Right-click on the sheet tab and "View Code".

Copy/paste the above into that sheet module.

As written it works only on column A. Adjust to suit.


Gord Dibben MS Excel MVP
 
G

Guest

Gord,
Thanks for the answer. This works perfect. A couple of follow up question:
1-How can I color the text the same as the cell fill so the text is not seen?
2-Where can I find out what number point to which color?
Thanks again for your help.
Cheryl
 
G

Gord Dibben

Add a line as below for font color to match background

'Apply the color
rng.Interior.ColorIndex = Num
' add this line rng.Font.ColorIndex = Num
Next rng

For a list of color indices see David McRitchie's site

http://www.mvps.org/dmcritchie/excel/colors.htm

Or run this macro to get a list on new worksheet.

Sub ListColorIndexes()
Dim Ndx As Long
Sheets.Add
For Ndx = 1 To 56
Cells(Ndx, 1).Interior.ColorIndex = Ndx
Cells(Ndx, 2).Value = Hex(ThisWorkbook.Colors(Ndx))
Cells(Ndx, 3).Value = Ndx
Next Ndx
End Sub


Gord
 
G

Guest

Perfect. Thanks So Much.


Gord Dibben said:
Add a line as below for font color to match background

'Apply the color
rng.Interior.ColorIndex = Num
' add this line rng.Font.ColorIndex = Num
Next rng

For a list of color indices see David McRitchie's site

http://www.mvps.org/dmcritchie/excel/colors.htm

Or run this macro to get a list on new worksheet.

Sub ListColorIndexes()
Dim Ndx As Long
Sheets.Add
For Ndx = 1 To 56
Cells(Ndx, 1).Interior.ColorIndex = Ndx
Cells(Ndx, 2).Value = Hex(ThisWorkbook.Colors(Ndx))
Cells(Ndx, 3).Value = Ndx
Next Ndx
End Sub


Gord
 
G

Gord Dibben

Appreciate the feedback.

Just a note on colorindex numbers.

You may have modified a color or two in your workbook.

Excel uses the colorindex of the original color, not the modified one.

i.e. Default Yellow = 6

Default Yellow is RGB 255, 255, 0

Change to green with 0, 255, 150 and Excel still calls it index number 6


Gord
 
G

Guest

Your solution works great for one set of conditions in a sheet, but is it
possible to make it work with multiple conditions? For example:

Range("a1:a10,a20:a30"))
Case Is = "": Num = 2 'white
Case Is = 0: Num = 38 'red
Case Is = 1: Num = 36 'yellow
Case Is = 2: Num = 35 'green
Case Is = 3: Num = 34 'blue

Range("b15:b30,b55:b60"))
Case Is = "": Num = 2 'white
Case Is < 90: Num = 38 'red
Case Is < 80: Num = 36 'yellow
Case Is < 70: Num = 35 'green
Case Is < 50: Num = 34 'blue

TIA.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top