changing the color of a cell acording to its value

D

Dani Lima

Hi.. I wanna a macro that can change the color of a cell acording to its
value. I've tried conditional formating, but it doesnt work cause I have 5
ranges of values.

from 0% to 79% = black with white font
from 80% to 84% = red with white font
from 85% to 87% = white with red font.
from 88% to 92% = yellow with red font
from 92% to 100% = green with white font

Could you helpme??
 
L

Luke M

This will apply formatting to every cell in your current selection (I.e., if
you select range A1:D4, formatting will be applied to those 16 cells)

Sub CellColor()
for each cell in selection
If Cell.Value >= 0 And Cell.Value <= 0.79 Then
'Black with white
Selection.Font.ColorIndex = 2
Selection.Interior.ColorIndex = 1
End If
If Cell.Value > 0.79 And Cell.Value <= 0.84 Then
'Red with white
Selection.Font.ColorIndex = 2
Selection.Interior.ColorIndex = 3
End If
If Cell.Value > 0.84 And Cell.Value <= 0.87 Then
'White with red
Selection.Font.ColorIndex = 3
Selection.Interior.ColorIndex = 2
End If
If Cell.Value > 0.87 And Cell.Value <= 0.92 Then
'Yellow with red
Selection.Font.ColorIndex = 3
Selection.Interior.ColorIndex = 6
End If
If Cell.Value > 0.92 And Cell.Value <= 1 Then
'Green with white
Selection.Font.ColorIndex = 2
Selection.Interior.ColorIndex = 10
End If
next cell
End Sub
 
D

Dani Lima

Hi, Luke!

I am trying to change the color of just one cell of one specific worksheet,
in this case "S43", I tried to change "cell.value" to "range("S43").value",
but it doesn't work with me...

"Luke M" escreveu:
 
D

Dani Lima

Hi, Luke!

I am trying to change a specific cell of a specific worksheet, I tried to
change your macro, writing "range("S43").value" where is write cell.value but
doesn't work for me.

Dani Lima

"Luke M" escreveu:
 

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