Vba for 5 cond format in a range

Spraggs

Well-known Member
Joined
Jan 19, 2007
Messages
704
Hello All,
I'm after some Vba because I require five conditional formats and I'm working with excel 2003.

My range is C3:BJ37 and cell H43 (Sheet1)

Conditions..
1. <=420 Colour Red
2. <=840 Colour Orange
3. <=1260 Colour Yellow
4. <=1680 Colour Light Green
5. <=21000 Colour Bright Green

(Colours not too important)

Is this possible.

Any help is appreciated.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi. Try this

Code:
Sub Colours()
Dim c As Range
For Each c In Range("C3:BJ37,H43")
    Select Case c.Value
        Case Is <= 420: c.Interior.ColorIndex = 3
        Case Is <= 840: c.Interior.ColorIndex = 44
        Case Is <= 1260: c.Interior.ColorIndex = 6
        Case Is <= 1680: c.Interior.ColorIndex = 43
        Case Is <= 21000: c.Interior.ColorIndex = 4
    End Select
Next c
End Sub
 
Upvote 0
Thank You.
Is this entered by Alt + F11 ? If so do I have to run a macro after any changes.
 
Upvote 0
If you want it to run automatically, do the cells contain formulas or do you change them manually?
 
Upvote 0
Peter,
The only cell that changes is H43 and this is automatically (by formula) the rest are fixed. Could I have it where the text in cells C3:BJ37 are the same colour as fill (hence text not visible). Thanks for your time.
 
Upvote 0
Try this: right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_Calculate()
With Range("H43")
    Select Case .Value
        Case Is <= 420: .Interior.ColorIndex = 3
        Case Is <= 840: .Interior.ColorIndex = 44
        Case Is <= 1260: .Interior.ColorIndex = 6
        Case Is <= 1680: .Interior.ColorIndex = 43
        Case Is <= 21000: .Interior.ColorIndex = 4
    End Select
End With
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
For Each c In Range("C3:BJ37,H43")
    Select Case c.Value
        Case Is <= 420: c.Interior.ColorIndex = 3: c.Font.ColorIndex = 3
        Case Is <= 840: c.Interior.ColorIndex = 44: c.Font.ColorIndex = 44
        Case Is <= 1260: c.Interior.ColorIndex = 6: c.Font.ColorIndex = 6
        Case Is <= 1680: c.Interior.ColorIndex = 43: c.Font.ColorIndex = 43
        Case Is <= 21000: c.Interior.ColorIndex = 4: c.Font.ColorIndex = 4
    End Select
Next c
End Sub
 
Upvote 0
It works for me. Perhaps events are disabled. In the code window press CTRL + G then in the Immediate Window type

Application.EnableEvents=True

and press Enter.

The code will only run when the sheet calculates or you change a value.
 
Upvote 0
Got this working fine Peter.

What happens if I need to change these values?
Tried changing and saving but colours didn't change.

Thanks Jase
 
Upvote 0
If you mean that you've changed the values in the code, you need to change the value in any cell to trigger the code run.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

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