Excel 2003: Using a formula to copy formatting (ie: background colour)

Hey there - Conditional Formatting is not cutting it for me right now because of the 3 conditions limit. I would like to shy away from a VB=based solution, because:
A) I don't want any issues when sharing my worksheet
B) I don't want to have to run a macro each time I change a value

Essentially what I'm hoping I can do is:
- On Worksheet A, put a different background colour in each cell (A1..A5)
- On Worksheet B, I have a formula in a cell that looks at the number in that cell (1 to 5) and based on that number, use the colour from the corresponding cell in Worksheet A
- eg: in Worksheet B, if I put a "2" in the cell, it will fill that cell with the colour found in WorksheetA:A2; if I put a "5" in the cell, it will fill the cell with the colour from WorksheetA:A5.

The problem is, I haven't found any functions in Excel that deal with the formatting of a cell, as opposed to the data.

Please bear in mind this is Excel 2003, so I can't use conditional formatting, since I need more than 3 different colours.

Thanks for you advice!
LVL 14
tomaugerdotcomAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Connect With a Mentor Microsoft MVP ExcelCommented:
Try this: On Sheet2, format cells A1 to A5 with the color of your choice.

Then plug this macro into Sheet1 and enter numbers into column A

Note that this is not dynamic, i.e. if you change the colors on Sheet2, only the cells changed after that will be affected. Cells already colored will keep their color. To apply the new color to all present cells, copy the cells and paste them again onto themselves. This will trigger the change event and the new colors will be applied.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
If Not Intersect(Target, Range("A:A")) Is Nothing Then
For Each cel In Target
    Select Case cel.Value
        Case 1
            With cel.Interior
                .ColorIndex = ActiveWorkbook.Worksheets("Sheet2").Range("A1").Interior.ColorIndex
                .Pattern = xlSolid
            End With
        Case 2
            With cel.Interior
                .ColorIndex = ActiveWorkbook.Worksheets("Sheet2").Range("A2").Interior.ColorIndex
                .Pattern = xlSolid
            End With
        Case 3
            With cel.Interior
                .ColorIndex = ActiveWorkbook.Worksheets("Sheet2").Range("A3").Interior.ColorIndex
                .Pattern = xlSolid
            End With
        Case 4
            With cel.Interior
                .ColorIndex = ActiveWorkbook.Worksheets("Sheet2").Range("A4").Interior.ColorIndex
                .Pattern = xlSolid
            End With
        Case 5
            With cel.Interior
                .ColorIndex = ActiveWorkbook.Worksheets("Sheet2").Range("A5").Interior.ColorIndex
                .Pattern = xlSolid
            End With
        Case Else
            cel.Interior.ColorIndex = xlNone
    End Select
Next cel
End If
End Sub

Open in new window


cheers, teylyn
0
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Hello,

there is no way to do what you describe with formulas. Conditional formatting is the only way to change cell formatting on the fly. Anything that goes beyond that will need to use VBA.

cheers, teylyn
0
 
tomaugerdotcomAuthor Commented:
Dang. Okay, so what would a VBA solution look like? And would it be automatic or would you have to push a button to make it update?

T
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
There are many ways to do this. It really depends on your requirements. The code below monitors column A. When any value from 1-5 is entered into column A, the cell will automatically turn one of five pre-defined colors.

The code goes into the sheet module (right-click the sheet tab, select View Code and paste code into the code window)

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
If Not Intersect(Target, Range("A:A")) Is Nothing Then
For Each cel In Target
    Select Case cel.Value
        Case 1
            With cel.Interior
                .ColorIndex = 43
                .Pattern = xlSolid
            End With
        Case 2
            With cel.Interior
                .ColorIndex = 8
                .Pattern = xlSolid
            End With
        Case 3
            With cel.Interior
                .ColorIndex = 6
                .Pattern = xlSolid
            End With
        Case 4
            With cel.Interior
                .ColorIndex = 53
                .Pattern = xlSolid
            End With
        Case 5
            With cel.Interior
                .ColorIndex = 55
                .Pattern = xlSolid
            End With
        Case Else
            cel.Interior.ColorIndex = xlNone
    End Select
Next cel
End If
End Sub

Open in new window


cheers, teylyn
0
 
tomaugerdotcomAuthor Commented:
Right on - thanks for this. Could you show me how to read the ColorIndex of a particular cell, rather than arbitrarily defining it within the code?


PS: Lol - VB is such a crappy language. This syntax: "If Not Intersect(Target, Range("A:A")) Is Nothing Then" is outrageous!

 
0
 
tomaugerdotcomAuthor Commented:
Outstanding, Teylyn - thanks so much for providing this excellent help. You really put me on the right track with this - and I like how it still pretty much updates automatically, so long as you don't mess with the colour codes.

I have improved on your code by making it more parametrized, and opening it up to using the full A:Z range of the worksheet. For anyone else wanting to use this code, you must name the worksheet that contains the colour reference "Colour Codes" (note the Canadian spelling!! hehe).

Well done and thanks.

-- Tom
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
    If Not Intersect(Target, Range("A:Z")) Is Nothing Then
        For Each cel In Target
            With cel.Interior
                .ColorIndex = ActiveWorkbook.Worksheets("Colour Codes").Cells(cel.Value, 1).Interior.ColorIndex
                .Pattern = xlSolid
            End With
        Next cel
    End If
End Sub

Open in new window

0
 
tomaugerdotcomAuthor Commented:
Thanks for taking the time and for providing working code.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.