Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 114
Default assigning different background colours in a range

Hi,

If I want to loop through a range (A1:D10) and look for different
values (A, B, C, D etc), how would I assign a background colour
according to the value?

Would it be a select case option?

Also, instead of stating the colour as FFFFFF etc could you use
standard colour names such as Red, Blue, Black etc?

kind regards,
Matt
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default assigning different background colours in a range

You could try something like the following:

Sub ColorCells()
Dim Rng As Range
Const RED = 3
Const GREEN = 4
Const BLUE = 5
Const YELLOW = 6

For Each Rng In Range("A1:C5").Cells
Select Case UCase(Rng.Text)
Case "A"
Rng.Interior.ColorIndex = RED
Case "B"
Rng.Interior.ColorIndex = GREEN
Case "C"
Rng.Interior.ColorIndex = BLUE
Case "D"
Rng.Interior.ColorIndex = YELLOW
''''''''''''''''''''''''''
' other Cases go here
''''''''''''''''''''''''''
Case Else
'''''''''''''''
' do nothing
'''''''''''''''
End Select
Next Rng
End Sub

To get the colors available, run the following code. The row number of each
color is the ColorIndex that is assigned to the Interior of a cell. E.g.,
row 6 has a yellow fill, so 6 is the ColorIndex that refers to yellow. Set
up constants for all the colors you want to use, as shown in the example
code.

Sub ListColorIndexs()
Dim WS As Worksheet
Dim N As Long
Set WS = Worksheets.Add
For N = 1 To 56
Cells(N, 1).Interior.ColorIndex = N
Next N
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"MJKelly" wrote in message
...
Hi,

If I want to loop through a range (A1:D10) and look for different
values (A, B, C, D etc), how would I assign a background colour
according to the value?

Would it be a select case option?

Also, instead of stating the colour as FFFFFF etc could you use
standard colour names such as Red, Blue, Black etc?

kind regards,
Matt


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default assigning different background colours in a range

While I was dragging my feet preparing an answer for this I see that Chip has
already answered. However, one other thing you might need is to be able to
remove the interior colorindex:-

Rng.Interior.ColorIndex = xlColorIndexNone

--
Regards,

OssieMac


"Chip Pearson" wrote:

You could try something like the following:

Sub ColorCells()
Dim Rng As Range
Const RED = 3
Const GREEN = 4
Const BLUE = 5
Const YELLOW = 6

For Each Rng In Range("A1:C5").Cells
Select Case UCase(Rng.Text)
Case "A"
Rng.Interior.ColorIndex = RED
Case "B"
Rng.Interior.ColorIndex = GREEN
Case "C"
Rng.Interior.ColorIndex = BLUE
Case "D"
Rng.Interior.ColorIndex = YELLOW
''''''''''''''''''''''''''
' other Cases go here
''''''''''''''''''''''''''
Case Else
'''''''''''''''
' do nothing
'''''''''''''''
End Select
Next Rng
End Sub

To get the colors available, run the following code. The row number of each
color is the ColorIndex that is assigned to the Interior of a cell. E.g.,
row 6 has a yellow fill, so 6 is the ColorIndex that refers to yellow. Set
up constants for all the colors you want to use, as shown in the example
code.

Sub ListColorIndexs()
Dim WS As Worksheet
Dim N As Long
Set WS = Worksheets.Add
For N = 1 To 56
Cells(N, 1).Interior.ColorIndex = N
Next N
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"MJKelly" wrote in message
...
Hi,

If I want to loop through a range (A1:D10) and look for different
values (A, B, C, D etc), how would I assign a background colour
according to the value?

Would it be a select case option?

Also, instead of stating the colour as FFFFFF etc could you use
standard colour names such as Red, Blue, Black etc?

kind regards,
Matt


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
how do I set all my worksheet background colours to white? CD New Users to Excel 2 May 8th 08 11:29 AM
Background colours... Mats K[_2_] Excel Programming 3 July 4th 07 12:26 PM
Background colours whatzzup Excel Discussion (Misc queries) 0 February 12th 07 02:38 PM
Row background colours dramajuana Excel Discussion (Misc queries) 6 June 11th 06 01:15 AM
How can I get different templates for chart background colours Fred Charts and Charting in Excel 1 March 17th 05 04:59 PM


All times are GMT +1. The time now is 10:23 PM.

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

About Us

"It's about Microsoft Excel"