Conditional Formating more than 3 things

weiro

New Member
Joined
Nov 25, 2005
Messages
7
I am trying to condional format some cells with text in so that H changes to one colour and S changes to anouther. I can do this using conditional formating but can only do three conditions.

I found this on here

'START OF CODE
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim WatchRange As Range
Dim CellVal As Integer
If Target.Cells.Count > 1 Then Exit Sub
If Target = "" Or Not IsNumeric(Target) Then Exit Sub
CellVal = Target
Set WatchRange = Range("A1:A10")

If Not Intersect(Target, WatchRange) Is Nothing Then
Select Case CellVal
Case 0 To 10
Target.Interior.ColorIndex = 5
Case 10 To 20
Target.Interior.ColorIndex = 10
Case 21 To 30
Target.Interior.ColorIndex = 6
Case 31 To 40
Target.Interior.ColorIndex = 46
Case 41 To 50
Target.Interior.ColorIndex = 45
End Select
End If
End Sub
'END OF CODE

but this is set up to work with numbers not text. Can anyone help change it or give me anouther way
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
You have only mentioned 2 conditions, H and S. You've said you want to program for more than 3, so what are all the conditions then?
 
Upvote 0
It is just lots of letters. They are in fact. H, S, N, HDH and HDS but I may want to add more to the list.

Thanks
 
Upvote 0
And what's the range to cover? The code you posted will colour cells A1:A10.
 
Upvote 0
It will vary as I need to do it on differnt sheets. A1 to A10 will do for now as i can change it later.

Thanks
 
Upvote 0
Start with this then:
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim WatchRange As Range
    Dim CellVal As String
    If Target.Cells.Count > 1 Then Exit Sub
    If Target = "" Or IsNumeric(Target) Then Exit Sub
    CellVal = Target
    Set WatchRange = Range("A1:A10")
    
    If Not Intersect(Target, WatchRange) Is Nothing Then
        Select Case CellVal
        Case "H"
            Target.Interior.ColorIndex = 5
        Case "S"
            Target.Interior.ColorIndex = 10
        Case "N"
            Target.Interior.ColorIndex = 6
        Case "HDH"
            Target.Interior.ColorIndex = 46
        Case "HDS"
            Target.Interior.ColorIndex = 45
        End Select
    End If
End Sub
 
Upvote 0
Sorry that seems to throw an error.

I am just copying it into the veiw code of the sheet.

Am I doing something wrong
 
Upvote 0
And what error exactly, would that be???????
 
Upvote 0
This is also something I've been looking for, BUT if I enter a letter not in the list or leave the cell blank by, say, deleting 'H' from a cell, I would want the colour to be removed from the cell.

Also what would be the code if I wanted to make the text in the cell Bold as well.
 
Upvote 0

Forum statistics

Threads
1,206,811
Messages
6,074,998
Members
446,112
Latest member
nmz1133

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