Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
My View
 
Posts: n/a
Default Cell color based upon cell value

I know you can use Conditional Formatting to allocate a colour for a cell
value but you are limited to 4 alternatives (ie the default cell color and
only 3 variations in Conditional Formatting).

How can I allocate a different color for 5 different cell values?

For example:
If cell value = 1 then cell color is green
If cell value = 2 then cell color is blue
If cell value = 3 then cell color is yellow
If cell value = 4 then cell color is red
If cell value = 5 then cell color is magenta

regards

PeterH


  #9   Report Post  
Gord Dibben
 
Posts: n/a
Default

View

Sheet event code can do the trick.....

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Num As Long
Dim rng As Range
Dim vRngInput As Variant
Set vRngInput = Intersect(Target, Range("A:A"))
If vRngInput Is Nothing Then Exit Sub
On Error GoTo endit
Application.EnableEvents = False
For Each rng In vRngInput
'Determine the color
Select Case rng.Value
Case Is = 1: Num = 10 'green
Case Is = 2: Num = 5 'blue
Case Is = 3: Num = 6 'yellow
Case Is = 4: Num = 3 'red
Case Is = 5: Num = 7 'magenta
End Select
'Apply the color
rng.Interior.ColorIndex = Num
Next rng
endit:
Application.EnableEvents = True
End Sub


Gord Dibben Excel MVP

On Mon, 04 Jul 2005 08:58:39 GMT, "My View" <reply to
wrote:

A macro would be fine. Whatever is easiest.


"Microsoft" wrote in message
...
Hi Peter,

You can do that with macros. If you want to do it with macros just let me
know. Otherwise you could try recording a macro first, and modifying the
generated code.

Shafiee.

"My View" <reply to
wrote in message
...
I know you can use Conditional Formatting to allocate a colour for a cell
value but you are limited to 4 alternatives (ie the default cell color

and
only 3 variations in Conditional Formatting).

How can I allocate a different color for 5 different cell values?

For example:
If cell value = 1 then cell color is green
If cell value = 2 then cell color is blue
If cell value = 3 then cell color is yellow
If cell value = 4 then cell color is red
If cell value = 5 then cell color is magenta

regards

PeterH






  #10   Report Post  
My View
 
Posts: n/a
Default

Thanks Gordon
For a person who has very little contact with Excel macros can you tell me
how to set-up the macro.
Also I only want the macro to work on certain cell ranges eg cells B4:B20
and D4:D20 and F4:F20 etc. How will this happen with a macro?
regards
PeterH


"Gord Dibben" <gorddibbATshawDOTca wrote in message
...
View

Sheet event code can do the trick.....

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Num As Long
Dim rng As Range
Dim vRngInput As Variant
Set vRngInput = Intersect(Target, Range("A:A"))
If vRngInput Is Nothing Then Exit Sub
On Error GoTo endit
Application.EnableEvents = False
For Each rng In vRngInput
'Determine the color
Select Case rng.Value
Case Is = 1: Num = 10 'green
Case Is = 2: Num = 5 'blue
Case Is = 3: Num = 6 'yellow
Case Is = 4: Num = 3 'red
Case Is = 5: Num = 7 'magenta
End Select
'Apply the color
rng.Interior.ColorIndex = Num
Next rng
endit:
Application.EnableEvents = True
End Sub


Gord Dibben Excel MVP

On Mon, 04 Jul 2005 08:58:39 GMT, "My View" <reply to


wrote:

A macro would be fine. Whatever is easiest.


"Microsoft" wrote in message
...
Hi Peter,

You can do that with macros. If you want to do it with macros just let

me
know. Otherwise you could try recording a macro first, and modifying

the
generated code.

Shafiee.

"My View" <reply to
wrote in message
...
I know you can use Conditional Formatting to allocate a colour for a

cell
value but you are limited to 4 alternatives (ie the default cell

color
and
only 3 variations in Conditional Formatting).

How can I allocate a different color for 5 different cell values?

For example:
If cell value = 1 then cell color is green
If cell value = 2 then cell color is blue
If cell value = 3 then cell color is yellow
If cell value = 4 then cell color is red
If cell value = 5 then cell color is magenta

regards

PeterH










  #11   Report Post  
Gord Dibben
 
Posts: n/a
Default

Peter

Amended code to cover the three ranges specified. The "etc." you can add.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Num As Long
Dim rng As Range
Dim vRngInput As Variant
Set vRngInput = Intersect(Target, Range("B4:B20, D4:D20, F4:F20"))
If vRngInput Is Nothing Then Exit Sub
On Error GoTo endit
Application.EnableEvents = False
For Each rng In vRngInput
'Determine the color
Select Case rng.Value
Case Is = 1: Num = 10 'green
Case Is = 2: Num = 5 'blue
Case Is = 3: Num = 6 'yellow
Case Is = 4: Num = 3 'red
Case Is = 5: Num = 7 'magenta
End Select
'Apply the color
rng.Interior.ColorIndex = Num
Next rng
endit:
Application.EnableEvents = True
End Sub

This is worksheet event code.

Right-click on the sheet tab and "View Code".

Copy/paste the code in there. Runs whenever a change is made to one of the
cells in the ranges.


Gord

On Tue, 05 Jul 2005 12:02:41 GMT, "My View" <reply to
wrote:

Thanks Gordon
For a person who has very little contact with Excel macros can you tell me
how to set-up the macro.
Also I only want the macro to work on certain cell ranges eg cells B4:B20
and D4:D20 and F4:F20 etc. How will this happen with a macro?
regards
PeterH


"Gord Dibben" <gorddibbATshawDOTca wrote in message
.. .
View

Sheet event code can do the trick.....

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Num As Long
Dim rng As Range
Dim vRngInput As Variant
Set vRngInput = Intersect(Target, Range("A:A"))
If vRngInput Is Nothing Then Exit Sub
On Error GoTo endit
Application.EnableEvents = False
For Each rng In vRngInput
'Determine the color
Select Case rng.Value
Case Is = 1: Num = 10 'green
Case Is = 2: Num = 5 'blue
Case Is = 3: Num = 6 'yellow
Case Is = 4: Num = 3 'red
Case Is = 5: Num = 7 'magenta
End Select
'Apply the color
rng.Interior.ColorIndex = Num
Next rng
endit:
Application.EnableEvents = True
End Sub


Gord Dibben Excel MVP

On Mon, 04 Jul 2005 08:58:39 GMT, "My View" <reply to


wrote:

A macro would be fine. Whatever is easiest.


"Microsoft" wrote in message
...
Hi Peter,

You can do that with macros. If you want to do it with macros just let

me
know. Otherwise you could try recording a macro first, and modifying

the
generated code.

Shafiee.

"My View" <reply to
wrote in message
...
I know you can use Conditional Formatting to allocate a colour for a

cell
value but you are limited to 4 alternatives (ie the default cell

color
and
only 3 variations in Conditional Formatting).

How can I allocate a different color for 5 different cell values?

For example:
If cell value = 1 then cell color is green
If cell value = 2 then cell color is blue
If cell value = 3 then cell color is yellow
If cell value = 4 then cell color is red
If cell value = 5 then cell color is magenta

regards

PeterH








  #12   Report Post  
 
Posts: n/a
Default

Gord
Thanks - that works perfectly.
regards
PeterH

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
cell color index comparison MINAL ZUNKE New Users to Excel 1 June 30th 05 07:11 AM
Cell Change Color - Need Help alani New Users to Excel 3 June 29th 05 03:50 PM
Pasting Word table cell with paragraph markers into single Excel c Steve Excel Discussion (Misc queries) 1 June 16th 05 11:26 PM
inserting data from a row to a cell, when the row number is specified by a formula in a cell [email protected] New Users to Excel 2 January 6th 05 08:18 AM
VLookup resulting in a blank cell... KempensBoerke Excel Worksheet Functions 1 October 28th 04 09:57 PM


All times are GMT +1. The time now is 12:01 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"