Conditional Formatting 6 conditions

Brian from Maui

MrExcel MVP
Joined
Feb 16, 2002
Messages
8,459
Aloha,

Can anyone write a code that highlights a row a different color. As an example. Range is A2:K100. If A2="Criteria 1" color A2:K2 green. If A2="Criteria 2" color A2:K2 yellow...etc etc.

TIA
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Somthing like this
Code:
Sub test()
Dim myColor As Integer, r As Range
For Each r In Range("a2:a100")
     myColor = xlNone
     Select Case r.Value
          Case "Criteria1"
               myColor = 4
          Case "Criteria2"
               myColor = 6
          Case "Criteria3"
               myColor = 3
          Case "myCriteria4"
               myColor = 7
          Case myCriteria5"
               myColor = 14
          Case "myCriteria6"
               myColor = 19
     End Select
     r.Resize(,11).Interior.ColorIndex = myColor
Next
End Sub
 
Upvote 0
Untested, but something along these lines should work:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Variant

If Intersect(Target, [A2:A100]) Is Nothing Then Exit Sub

Select Case Target.Value
    Case Is = "Criteria 1"
        c = 'colorindex for green (don't know it off the top of my head)
    Case Is = "Criteria 2"
        c = 6 'colorindex for yellow
    Case Else 'if the value of the cell doesn't match any of the above cases
        c = xlNone 'no color
End Select

'change color of A:K of the current row
Range(Target, Target.Offset(0, 10)).Interior.ColorIndex = c

End Sub
 
Upvote 0
It doesn't fire. I tried entering in a module worksheet and workbook. Nothing fires. To clarify. My range is A2:K100. If A2="a", color A2:K2 red.

If A3 is "b", color A3:K3 blue and so on for six criterias.
 
Upvote 0
The code Jindon posted does not fire automatically--you have to run it yourself.

The code I posted is a Worksheet_Change event, so it would need to go in the module of the sheet you want it to work on. As posted, it is written so that it will only fire if you make a change in A2:A100, and will not be triggered if the values are changing due to formulas in them--it has to be a manual change. To trigger a macro from a formula result, you can use a Calculate event instead of Change event.
 
Upvote 0
Untested, but something along these lines should work:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Variant

If Intersect(Target, [A2:A100]) Is Nothing Then Exit Sub

Select Case Target.Value
    Case Is = "Criteria 1"
        c = 'colorindex for green (don't know it off the top of my head)
    Case Is = "Criteria 2"
        c = 6 'colorindex for yellow
    Case Else 'if the value of the cell doesn't match any of the above cases
        c = xlNone 'no color
End Select

'change color of A:K of the current row
Range(Target, Target.Offset(0, 10)).Interior.ColorIndex = c

End Sub

Thanks Kristy, that works.... :LOL:
 
Upvote 0
Yeah, because I didn't know if the cells are changed by manually or formula...
To the sheet module
If you are changing manually
Code:
Private Sub worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("a2:a100")) Is Nothing Then test
End Sub

If by the formula then
Code:
Private Sub Worksheet_Calculate()
test
End Sub
 
Upvote 0
Yeah, because I didn't know if the cells are changed by manually or formula...
To the sheet module
If you are changing manually
Code:
Private Sub worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("a2:a100")) Is Nothing Then test
End Sub

If by the formula then
Code:
Private Sub Worksheet_Calculate()
test
End Sub

Thanks Jindon!
 
Upvote 0
Jindon,

Like this????

Code:
Private Sub worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("a2:a100")) Is Nothing Then test
End Sub
For Each r In Range("a2:a100")
     myColor = xlNone
     Select Case r.Value
          Case "a"
               myColor = 4
          Case "b"
               myColor = 6
          Case "Criteria3"
               myColor = 3
          Case "myCriteria4"
               myColor = 7
          Case "myCriteria5"
               myColor = 14
          Case "myCriteria6"
               myColor = 19
     End Select
     r.Resize(, 11).Interior.ColorIndex = myColor
Next
End Sub
 
Upvote 0
Change like this, so that it will run correctly when you paste/change the multiple data at a time.
Code:
Private Sub worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("a2:a100")) Is Nothing Then _
       test Intersect(Target, Range("a2:a100"))
End Sub

Sub test(ByVal rng As Range)
Dim r As Range, myColor As Integer
For Each r In rng
     myColor = xlNone
     Select Case r.Value
          Case "a"
               myColor = 4
          Case "b"
               myColor = 6
          Case "Criteria3"
               myColor = 3
          Case "myCriteria4"
               myColor = 7
          Case "myCriteria5"
               myColor = 14
          Case "myCriteria6"
               myColor = 19
     End Select
     r.Resize(, 11).Interior.ColorIndex = myColor
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,739
Members
448,989
Latest member
mariah3

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