Solved

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

Posted on 2010-11-25
7
664 Views
Last Modified: 2012-05-10
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!
0
Comment
Question by:tomaugerdotcom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 50
ID: 34214182
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
 
LVL 14

Author Comment

by:tomaugerdotcom
ID: 34214190
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
 
LVL 50
ID: 34214256
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Author Comment

by:tomaugerdotcom
ID: 34214467
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
 
LVL 50

Accepted Solution

by:
Ingeborg Hawighorst (Microsoft MVP / EE MVE) earned 500 total points
ID: 34214728
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
 
LVL 14

Author Comment

by:tomaugerdotcom
ID: 34218650
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
 
LVL 14

Author Closing Comment

by:tomaugerdotcom
ID: 34218651
Thanks for taking the time and for providing working code.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question