Cange cell background colour based on contents of other cell

Dave_P_C

New Member
Joined
Nov 9, 2006
Messages
37
I have a spreadsheet which has text values in a left hand column and numeric values in the adjacent right hand column.

For example cells A1, C1, C3, C4, etc... could have the values "BH" or "SD" or "S" or "H" or "FH".

Cells B1, B2, B3, B4, etc... could have numeric values in them from 0.1 to 7.5.
Book1
ABCDEF
1H7.5FH2S7.5
2SD7.5SD3
3SD7.5
4
5S3.5
6FH3.5
7S7.5
8
9
10BH7.5BH7.5BH
Sheet1



I already have VBA script that will set the background colour of all cells containing a text values to a certain colour.

For example, if cells A1, A3, A5 and A6 contained "BH" they would all be given a yellow background. Cells containing the value "H" would get a green background, cells containing "S" a Grey background, cells containing "SD" a purple background and cells containing "F" a blue background.

What I need to do is set the cell to the right of one containing a specific value to the same background colour.

For example if cells A5, C5, and E5 all contain "BH" and have yellow backgrounds I need cells B5, D5 and F5 to also be given yellow backgrounds.

The working VB Script I have so far to set a cell colour depending on the contents is :-

Sub SetCellColour()

Dim cell As Range
Dim Target As Range

For Each cell In ActiveSheet.Range("C13:U163")
If cell.Value = "F" Then
cell.Interior.ColorIndex = 8
cell.Interior.Pattern = xlSolid
cell.Interior.PatternColorIndex = xlAutomatic
ElseIf cell.Value = "SD" Then
cell.Interior.ColorIndex = 7
cell.Interior.Pattern = xlSolid
cell.Interior.PatternColorIndex = xlAutomatic
ElseIf cell.Value = "S" Then
cell.Interior.ColorIndex = 15
cell.Interior.Pattern = xlSolid
cell.Interior.PatternColorIndex = xlAutomatic
ElseIf cell.Value = "BH" Then
cell.Interior.ColorIndex = 6
cell.Interior.Pattern = xlSolid
cell.Interior.PatternColorIndex = xlAutomatic
ElseIf cell.Value >= 0.1 Then
cell.Interior.ColorIndex = 4
cell.Interior.Pattern = xlSolid
cell.Interior.PatternColorIndex = xlAutomatic
ElseIf cell.Value = "" Then
cell.Interior.ColorIndex = 0
cell.Interior.Pattern = xlSolid
cell.Interior.PatternColorIndex = xlAutomatic
End If
Next

End Sub

Could someone please let me know what additional script would have to be added in order to set the background colour of a cell on the right hand side (e.g. B10) with that to the one on the left (e.g. A10).

I have tried using the cell.offset but without success and I am not even sure if this is the correct approach.

Any help you could provide will be greatly appreciated.

Many thanks.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
i had a similar problem the other week and here is the answer i got

try

Code:
Private Sub Worksheet_Calculate() 

m = [c1].Value 
n = [d1].Value 
o = [e1].Value 
p = [f1].Value 
q = [g1].Value 
r = [h1].Value 
s = [i1].Value 

Range("a8:r100").Interior.ColorIndex = 0 
    For Each c In Range("c8:r100") 
        Select Case c.Value 
            Case m 
                Range(c, c.Offset(, 1)).Interior.ColorIndex = 44 
            Case n 
                Range(c, c.Offset(, 1)).Interior.ColorIndex = 39 
            Case o 
                Range(c, c.Offset(, 1)).Interior.ColorIndex = 4 
            Case p 
                Range(c, c.Offset(, 1)).Interior.ColorIndex = 5 
            Case q 
                Range(c, c.Offset(, 1)).Interior.ColorIndex = 6 
            Case r 
                Range(c, c.Offset(, 1)).Interior.ColorIndex = 7 
            Case s 
                Range(c, c.Offset(, 1)).Interior.ColorIndex = 8 
            Case Else 
                iCol = 0 
        End Select 
Next c 

End Sub

you need to alter the colorinex to suit and the select case too

HTH
 
Upvote 0
something like

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
f = "f"
sd = "sd"
s = "s"
bh = "bh"

Range("a8:r100").Interior.ColorIndex = 0
    For Each c In Range("c8:r100")
        Select Case c.Value
            Case f
                Range(c, c.Offset(, 1)).Interior.ColorIndex = 44
            Case sd
                Range(c, c.Offset(, 1)).Interior.ColorIndex = 39
            Case s
                Range(c, c.Offset(, 1)).Interior.ColorIndex = 4
            Case bh
                Range(c, c.Offset(, 1)).Interior.ColorIndex = 5
            Case s
                Range(c, c.Offset(, 1)).Interior.ColorIndex = 4
            Case Else
                iCol = 0
        End Select
Next c

End Sub

also add extra "case" sections where needed
 
Upvote 0
Many thanks to shippey121 for his suggested code.

I have altered my code so that it now works as required.

My code as it stands (now working thanks to shippey121) is below.

Sub SetCellColour()

Dim cell As Range

For Each cell In ActiveSheet.Range("C13:U163")
If cell.Value = "F" Then
cell.Interior.ColorIndex = 8 'Light Blue
Range(cell, cell.Offset(, 1)).Interior.ColorIndex = 8 'Light Blue
ElseIf cell.Value = "SD" Then
cell.Interior.ColorIndex = 7 'Pink
Range(cell, cell.Offset(, 1)).Interior.ColorIndex = 7 'Pink
ElseIf cell.Value = "S" Then
cell.Interior.ColorIndex = 15 'Grey
Range(cell, cell.Offset(, 1)).Interior.ColorIndex = 15 'Grey
ElseIf cell.Value = "BH" Then
cell.Interior.ColorIndex = 6 'Yellow
Range(cell, cell.Offset(, 1)).Interior.ColorIndex = 6 'Yellow
ElseIf cell.Value = "H" Then
cell.Interior.ColorIndex = 4 'Green
Range(cell, cell.Offset(, 1)).Interior.ColorIndex = 4 'Green
ElseIf cell.Value = "" Then
cell.Interior.ColorIndex = 0 'Blank
' cell.Interior.Pattern = xlSolid
' cell.Interior.PatternColorIndex = xlAutomatic
End If
Next

End Sub
 
Upvote 0
DAVE

your code could be simplyfied to

Code:
Sub COLOR()
Dim cell As Range

For Each cell In ActiveSheet.Range("C13:U163")
If cell.Value = "F" Then
Range(cell, cell.Offset(, 1)).Interior.ColorIndex = 8 'Light Blue
ElseIf cell.Value = "SD" Then
Range(cell, cell.Offset(, 1)).Interior.ColorIndex = 7 'Pink
ElseIf cell.Value = "S" Then
Range(cell, cell.Offset(, 1)).Interior.ColorIndex = 15 'Grey
ElseIf cell.Value = "BH" Then
Range(cell, cell.Offset(, 1)).Interior.ColorIndex = 6 'Yellow
ElseIf cell.Value = "H" Then
Range(cell, cell.Offset(, 1)).Interior.ColorIndex = 4 'Green
ElseIf cell.Value = "" Then
cell.Interior.ColorIndex = 0 'Blank

End If
Next
End Sub
in your existing code

the line

cell.Interior.ColorIndex = 8 'Light Blue

is changing the cell which was being checked and then on the next line

Range(cell, cell.Offset(, 1)).Interior.ColorIndex = 8 'Light Blue

this is changing both cells, example

if C13 had the letter F, your code would change C13 to light blue then C13:D13 to light blue, remove all the lines starting with cell. and your code will run faster
 
Upvote 0

Forum statistics

Threads
1,216,081
Messages
6,128,694
Members
449,464
Latest member
againofsoul

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