Change shape color based on cell value in multiple columns

lai_arceo

New Member
Joined
Apr 7, 2010
Messages
35
Hello.

I want to change the color of the shapes in columns D, I, N, S, X and AC based on the values of columns E, J, O, T, Y and AD respectively. If a cell in latter columns is:

B - shape color should be red
K - shape color should be blue
BIN - shape color should be green

Your help will be much appreciated.

Sample File.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACAD
1
2BBBBBB
3
4
5
6KBBINKBINBIN
7
8
9
10
11KBBINKBINBIN
12
1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
R10Cell Value="K"textNO
R10Cell Value="B"textNO
P12Cell Value="K"textNO
P12Cell Value="B"textNO
P10:P11Cell Value="K"textNO
P10:P11Cell Value="B"textNO
AB10Cell Value="K"textNO
AB10Cell Value="B"textNO
Z10Cell Value="K"textNO
Z10Cell Value="B"textNO
Z12Cell Value="K"textNO
Z12Cell Value="B"textNO
Z11Cell Value="K"textNO
Z11Cell Value="B"textNO
W10Cell Value="K"textNO
W10Cell Value="B"textNO
U10Cell Value="K"textNO
U10Cell Value="B"textNO
U12Cell Value="K"textNO
U12Cell Value="B"textNO
U11Cell Value="K"textNO
U11Cell Value="B"textNO
M10Cell Value="K"textNO
M10Cell Value="B"textNO
K10Cell Value="K"textNO
K10Cell Value="B"textNO
K12Cell Value="K"textNO
K12Cell Value="B"textNO
K11Cell Value="K"textNO
K11Cell Value="B"textNO
H10Cell Value="K"textNO
H10Cell Value="B"textNO
C10Cell Value="K"textNO
C10Cell Value="B"textNO
F12Cell Value="K"textNO
F12Cell Value="B"textNO
A12Cell Value="K"textNO
A12Cell Value="B"textNO
F10Cell Value="K"textNO
F10Cell Value="B"textNO
F11Cell Value="K"textNO
F11Cell Value="B"textNO
A10:A11Cell Value="K"textNO
A10:A11Cell Value="B"textNO
B9:D9,G9:I9,L9:N9,V9:X9,AA9:AC9,Q9:S9Cell Value="K"textNO
B9:D9,G9:I9,L9:N9,V9:X9,AA9:AC9,Q9:S9Cell Value="B"textNO
R5Cell Value="K"textNO
R5Cell Value="B"textNO
P7Cell Value="K"textNO
P7Cell Value="B"textNO
P3Cell Value="K"textNO
P3Cell Value="B"textNO
P5:P6Cell Value="K"textNO
P5:P6Cell Value="B"textNO
P1:P2,R1Cell Value="K"textNO
P1:P2,R1Cell Value="B"textNO
AB5Cell Value="K"textNO
AB5Cell Value="B"textNO
Z5Cell Value="K"textNO
Z5Cell Value="B"textNO
AB1Cell Value="K"textNO
AB1Cell Value="B"textNO
Z1Cell Value="K"textNO
Z1Cell Value="B"textNO
Z7Cell Value="K"textNO
Z7Cell Value="B"textNO
Z6Cell Value="K"textNO
Z6Cell Value="B"textNO
Z3Cell Value="K"textNO
Z3Cell Value="B"textNO
Z2Cell Value="K"textNO
Z2Cell Value="B"textNO
W5Cell Value="K"textNO
W5Cell Value="B"textNO
U5Cell Value="K"textNO
U5Cell Value="B"textNO
W1Cell Value="K"textNO
W1Cell Value="B"textNO
U1Cell Value="K"textNO
U1Cell Value="B"textNO
U7Cell Value="K"textNO
U7Cell Value="B"textNO
U6Cell Value="K"textNO
U6Cell Value="B"textNO
U3Cell Value="K"textNO
U3Cell Value="B"textNO
U2Cell Value="K"textNO
U2Cell Value="B"textNO
M5Cell Value="K"textNO
M5Cell Value="B"textNO
K5Cell Value="K"textNO
K5Cell Value="B"textNO
M1Cell Value="K"textNO
M1Cell Value="B"textNO
K1Cell Value="K"textNO
K1Cell Value="B"textNO
H1Cell Value="K"textNO
H1Cell Value="B"textNO
K7Cell Value="K"textNO
K7Cell Value="B"textNO
K6Cell Value="K"textNO
K6Cell Value="B"textNO
H5Cell Value="K"textNO
H5Cell Value="B"textNO
C5Cell Value="K"textNO
C5Cell Value="B"textNO
F7Cell Value="K"textNO
F7Cell Value="B"textNO
A7Cell Value="K"textNO
A7Cell Value="B"textNO
K3Cell Value="K"textNO
K3Cell Value="B"textNO
F3Cell Value="K"textNO
F3Cell Value="B"textNO
A3Cell Value="K"textNO
A3Cell Value="B"textNO
F5Cell Value="K"textNO
F5Cell Value="B"textNO
K2Cell Value="K"textNO
K2Cell Value="B"textNO
F6Cell Value="K"textNO
F6Cell Value="B"textNO
F2Cell Value="K"textNO
F2Cell Value="B"textNO
F1Cell Value="K"textNO
F1Cell Value="B"textNO
A5:A6Cell Value="K"textNO
A5:A6Cell Value="B"textNO
A1:A2,C1Cell Value="K"textNO
A1:A2,C1Cell Value="B"textNO
B4:D4,G4:I4,L4:N4,V4:X4,AA4:AC4,Q4:S4Cell Value="K"textNO
B4:D4,G4:I4,L4:N4,V4:X4,AA4:AC4,Q4:S4Cell Value="B"textNO
 

Attachments

  • Sample file.JPG
    Sample file.JPG
    53 KB · Views: 6
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You can run the following macro to color all the shapes.

VBA Code:
Sub ShapeColor()
  Dim shp As Shape
  For Each shp In ActiveSheet.Shapes
    With shp.Fill.ForeColor
      Select Case shp.TopLeftCell.Offset(, 1).Value
        Case "B":   .RGB = RGB(255, 0, 0)
        Case "K":   .RGB = RGB(0, 0, 255)
        Case "BIN": .RGB = RGB(0, 255, 0)
      End Select
    End With
  Next
End Sub
HOW TO INSTALL MACROs
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (ShapeColor) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "Yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
------------------------------------

Or also put the following code in the events of your sheet. Every time you change the value of one of the cells, the shape will automatically change color.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim rng As Range, c As Range
  Dim shp As Shape
  
  Set rng = Intersect(Target, Range("E:E, J:J, O:O, T:T, Y:Y, AD:AD"))
  If Not rng Is Nothing Then
    For Each c In rng
      For Each shp In ActiveSheet.Shapes
        If Not Intersect(shp.TopLeftCell, c.Offset(, -1)) Is Nothing Then
          Select Case c.Value
            Case "B":   shp.Fill.ForeColor.RGB = RGB(255, 0, 0)
            Case "K":   shp.Fill.ForeColor.RGB = RGB(0, 0, 255)
            Case "BIN": shp.Fill.ForeColor.RGB = RGB(0, 255, 0)
          End Select
          Exit For
        End If
      Next
    Next
  End If
End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.
 
Upvote 0
You can run the following macro to color all the shapes.

VBA Code:
Sub ShapeColor()
  Dim shp As Shape
  For Each shp In ActiveSheet.Shapes
    With shp.Fill.ForeColor
      Select Case shp.TopLeftCell.Offset(, 1).Value
        Case "B":   .RGB = RGB(255, 0, 0)
        Case "K":   .RGB = RGB(0, 0, 255)
        Case "BIN": .RGB = RGB(0, 255, 0)
      End Select
    End With
  Next
End Sub
HOW TO INSTALL MACROs
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (ShapeColor) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "Yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
------------------------------------

Or also put the following code in the events of your sheet. Every time you change the value of one of the cells, the shape will automatically change color.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim rng As Range, c As Range
  Dim shp As Shape
 
  Set rng = Intersect(Target, Range("E:E, J:J, O:O, T:T, Y:Y, AD:AD"))
  If Not rng Is Nothing Then
    For Each c In rng
      For Each shp In ActiveSheet.Shapes
        If Not Intersect(shp.TopLeftCell, c.Offset(, -1)) Is Nothing Then
          Select Case c.Value
            Case "B":   shp.Fill.ForeColor.RGB = RGB(255, 0, 0)
            Case "K":   shp.Fill.ForeColor.RGB = RGB(0, 0, 255)
            Case "BIN": shp.Fill.ForeColor.RGB = RGB(0, 255, 0)
          End Select
          Exit For
        End If
      Next
    Next
  End If
End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.

This worked great! Thank you so much, @DanteAmor!
 
Upvote 0
@DanteAmor - I have another question though. The values "BIN,K,B" are results of a cell calculation instead of a manual input. How can I trigger the macro to run if "Worksheet_Calculate" is used instead of "Worksheet_Change"?
 
Upvote 0
The values "BIN,K,B" are results of a cell calculation instead of a manual input.
So which cells do you manually modify?
Put here the formulas that you use to obtain the BIN, K and B values.
 
Upvote 0
@DanteAmor Here's a copy of the table. Hope this helps.

STUDENT.xlsm
AB
11
1
Cell Formulas
RangeFormula
A1A1=LEFT(E3,1)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A1:A2,C1Cell Value="K"textNO
A1:A2,C1Cell Value="B"textNO




The values BIN, K & B are results based on the Student's ID number, entered in the cells, E1, J1, O1, T1, E5, J5, O5, T5....
 
Upvote 0
I do not understand. Which cells do you manually modify so that the B, K and BIM values appear?
 
Upvote 0
Try this:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim rng As Range, c As Range
  Dim shp As Shape
  
  Set rng = Intersect(Target, Range("E:E, J:J, O:O, T:T, Y:Y, AD:AD"), Range("1:1,5:5,10:10"))
  
  If Not rng Is Nothing Then
    For Each c In rng
      For Each shp In ActiveSheet.Shapes
        If Not Intersect(shp.TopLeftCell, c.Offset(1, -1)) Is Nothing Then
          Select Case c.Value
            Case "B":   shp.Fill.ForeColor.RGB = RGB(255, 0, 0)
            Case "K":   shp.Fill.ForeColor.RGB = RGB(0, 0, 255)
            Case "BIN": shp.Fill.ForeColor.RGB = RGB(0, 255, 0)
          End Select
          Exit For
        End If
      Next
    Next
  End If
End Sub
 
Upvote 0
Solution
Try this:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim rng As Range, c As Range
  Dim shp As Shape
 
  Set rng = Intersect(Target, Range("E:E, J:J, O:O, T:T, Y:Y, AD:AD"), Range("1:1,5:5,10:10"))
 
  If Not rng Is Nothing Then
    For Each c In rng
      For Each shp In ActiveSheet.Shapes
        If Not Intersect(shp.TopLeftCell, c.Offset(1, -1)) Is Nothing Then
          Select Case c.Value
            Case "B":   shp.Fill.ForeColor.RGB = RGB(255, 0, 0)
            Case "K":   shp.Fill.ForeColor.RGB = RGB(0, 0, 255)
            Case "BIN": shp.Fill.ForeColor.RGB = RGB(0, 255, 0)
          End Select
          Exit For
        End If
      Next
    Next
  End If
End Sub
Thank you so much, @DanteAmor!
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,578
Members
449,174
Latest member
chandan4057

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