Conditional Formatting Macro (5 conditions)

jberg123

New Member
Joined
Feb 24, 2011
Messages
45
Got a range G16:G40 with numbers (cell referenced).

Got three numbers in cells X, Y, Z.

I want cells i the range to be Green if cell value > X, Light Green if between Y and X, Orange if between Z and Y, Red if less than Z, and no format at all if cell is empty.

I can't use excel's standard conditional formatting since it can only handle 3 conditinos (or 4, if you will). I have 5.

Anyone have a good macro for this? Would be most helpful and appreciated.

Thanks!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Actually, can I modify this so that it colours THIS cells according to the integer value in the cell immediately to the right?


Private Sub Worksheet_Change(ByVal Target As Range)

Dim icolor As Integer



If Not Intersect(Target, Range("A1:A10")) is Nothing Then

Select Case Target

Case 1 To 5

icolor = 6

Case 6 To 10

icolor = 12

Case 11 To 15

icolor = 7

Case 16 To 20

icolor = 53

Case 21 To 25

icolor = 15

Case 26 To 30

icolor = 42

Case Else

'Whatever

End Select



Target.Interior.ColorIndex = icolor

End If
 
Upvote 0
Try

Rich (BB code):
Select Case Target.Offset(, 1)


Did the trick!!! Thanks!

Question now though.

This macro doesn't happen "continously all the time". I have to select cell, F2, ENTER, for colour to change... Can I change this somehow?

Thanks.



Private Sub Worksheet_Change(ByVal Target As Range)

Dim icolor As Integer



If Not Intersect(Target, Range("G16:G40")) Is Nothing Then

Select Case Target.Offset(, 1)

Case 1

icolor = 4

Case 2

icolor = 35

Case 3

icolor = 44

Case 4

icolor = 6

Case 5

icolor = 2

Case Else

'Whatever

End Select



Target.Interior.ColorIndex = icolor

End If

End Sub
 
Upvote 0
That should work automatically if you manually change values. Does that range contain formulas?
 
Upvote 0
Try replacing that with

Code:
Private Sub Worksheet_Calculate()
Dim icolor As Integer, c As Range
For Each c In Range("G16:G40")
    Select Case c.Offset(, 1)
        Case 1: icolor = 4
        Case 2: icolor = 35
        Case 3: icolor = 44
        Case 4: icolor = 6
        Case 5: icolor = 2
    End Select
    c.Interior.ColorIndex = icolor
Next c
End Sub
 
Upvote 0
Try replacing that with

Code:
Private Sub Worksheet_Calculate()
Dim icolor As Integer, c As Range
For Each c In Range("G16:G40")
    Select Case c.Offset(, 1)
        Case 1: icolor = 4
        Case 2: icolor = 35
        Case 3: icolor = 44
        Case 4: icolor = 6
        Case 5: icolor = 2
    End Select
    c.Interior.ColorIndex = icolor
Next c
End Sub

Brilliant! Many thanks!

One last question, I want to do this for G16:G40, H16:H40 and I16:I40 at the same time. How do I do this?
 
Upvote 0
Try

Rich (BB code):
Private Sub Worksheet_Calculate()
Dim icolor As Integer, c As Range
For Each c In Range("G16:I40")
    Select Case c.Offset(, 1)
        Case 1: icolor = 4
        Case 2: icolor = 35
        Case 3: icolor = 44
        Case 4: icolor = 6
        Case 5: icolor = 2
    End Select
    c.Interior.ColorIndex = icolor
Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

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