[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How can I use an IF statement to change cell background color format where more than 3 color options are needed?

Posted on 2011-03-14
11
Medium Priority
?
345 Views
Last Modified: 2012-05-11
Hello experts,

I need help formatting a cell to display a different color for each day of the week:

Mon      Yellow
Tue      Blue
Wed      Pink
Thu      Green
Fri      Gold

I’m thinking of something like this:

Cell A2 contains the day, e.g. Mon.  Cell B2 is a formatting formula for cell A2, e.g.:
=if(a2=“Mon”, cell.Interior.ColorIndex=Yellow)

Can I do this?  (Conditional formatting in my Excel 2003 limits me to 3 conditions.)
If not, is there a workaround?  (I would prefer not to use VBA.)

Thanks,
Gary
0
Comment
Question by:garyrobbins
[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
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35132319
Add a worksheet macro for example right click the tab then paste:

And no need for column B in this case

Chris
Private Sub Worksheet_Change(ByVal Target As Range)

    For Each Cell In intersect(Range("A2:A100"), target)
        If Cell.Value = "Mon" Then
            Cell.Interior.ColorIndex = 3
        ElseIf Cell.Value = "Tue" Then
            Cell.Interior.ColorIndex = 4
        ElseIf Cell.Value = "Wed" Then
            Cell.Interior.ColorIndex = 5
        ElseIf Cell.Value = "Thu" Then
            Cell.Interior.ColorIndex = 6
        ElseIf Cell.Value = "Fri" Then
            Cell.Interior.ColorIndex = 7
        ElseIf Cell.Value = "Sat" Then
            Cell.Interior.ColorIndex = 8
        ElseIf Cell.Value = "Sun" Then
            Cell.Interior.ColorIndex = 9
        Else
            Cell.Interior.ColorIndex = xlNone
        End If
    Next
End Sub

Open in new window

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35132410
Missed you stated the actual colurs:

Chris

Private Sub Worksheet_Change(ByVal Target As Range)

    For Each Cell In intersect(Range("A2:A100"), target)
        If Cell.Value = "Mon" Then
            Cell.Interior.ColorIndex = 65535
        ElseIf Cell.Value = "Tue" Then
            Cell.Interior.ColorIndex = 16711680
        ElseIf Cell.Value = "Wed" Then
            Cell.Interior.ColorIndex = 13353215
        ElseIf Cell.Value = "Thu" Then
            Cell.Interior.ColorIndex = 65280
        ElseIf Cell.Value = "Fri" Then
            Cell.Interior.ColorIndex = 55295
'        ElseIf Cell.Value = "Sat" Then
'            Cell.Interior.ColorIndex = 8
'        ElseIf Cell.Value = "Sun" Then
'            Cell.Interior.ColorIndex = 9
        Else
            Cell.Interior.ColorIndex = xlNone
        End If
    Next
End Sub

Open in new window

0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 1000 total points
ID: 35132492
Shows what happens when you don't test ... I used index and shouldn't!

The test range is limited to a1:a100 but of course change that as required

Chris
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
    For Each cel In Intersect(Me.Range("A1:A100"), Target)
        If cel.Value = "Mon" Then
            cel.Interior.Color = 65535
        ElseIf cel.Value = "Tue" Then
            cel.Interior.Color = 16711680
        ElseIf cel.Value = "Wed" Then
            cel.Interior.Color = 13353215
        ElseIf cel.Value = "Thu" Then
            cel.Interior.Color = 65280
        ElseIf cel.Value = "Fri" Then
            cel.Interior.Color = 55295
'        ElseIf cel.Value = "Sat" Then
'            cel.Interior.ColorIndex = 8
'        ElseIf cel.Value = "Sun" Then
'            cel.Interior.ColorIndex = 9
        Else
            cel.Interior.Color = xlNone
        End If
    Next
End Sub

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:garyrobbins
ID: 35132948
Thanks, Chris, for the Macro approach.

This will be a worksheet where rows will be added.  I want the cell color to show upon entry without running a macro each time.  Can I do this with a formula?

Gary
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35133011
The macro is dynamic so try it ... whenever you edit the cells the color changes.

Chris
0
 
LVL 39

Assisted Solution

by:nutsch
nutsch earned 1000 total points
ID: 35134279
A shorter version of Chris's code, just for fun. As he said, right click the tab, click View Code then paste.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
application.screenupdating=false
    For Each Cell In Intersect(Range("A2:A100"), Target).cells
        For i = 3 To 9
             If UCase(Cell) = UCase(Format(i, "ddd")) Then
                Cell.Interior.ColorIndex = i
                GoTo nxtCl
            Else
                Cell.Interior.ColorIndex = xlNone
            End If
        Next
nxtCl:
    Next
application.screenupdating=true
End Sub

Open in new window


Thomas
0
 
LVL 39

Expert Comment

by:nutsch
ID: 35134293
And to handle errors when you update non A cells:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
application.screenupdating=false

If Not Intersect(Target, Range("A2:A200")) Is Nothing Then
    For Each Cell In Intersect(Range("A2:A100"), Target)
        For i = 3 To 9
             If UCase(Cell) = UCase(Format(i, "ddd")) Then
                Cell.Interior.ColorIndex = i
                GoTo nxtCl
            Else
                Cell.Interior.ColorIndex = xlNone
            End If
        Next
nxtCl:
    Next
End If
application.screenupdating=true

End Sub

Open in new window

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35135300
garyrobbins

Since I was not explicit, you wanted to avoid using VBA and the bottom line is that cannot be done, (in 2003 whereas later versions allow more CF entries.  IN this case whatever approach you use will require a macro and the change event handler uses the built in methods so is perfect for the job.

Chris
0
 
LVL 24

Expert Comment

by:Tracy
ID: 35360839
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0
 

Author Comment

by:garyrobbins
ID: 35379176
I'm sorry for abandoning you all on this one.  I spent time trying the VBA solution but was not totally satisfied (especially with my comfort level with supporting VBA at this time).  I agree with your split, these suggestions, I believe are the answer.

Thanks,
Gary
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

656 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