Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Change color of Shape based on condition - Macro?

I need to have an auto shape, just a regular rectangle, change color based on
a cell next to it. The cell next to it contains an IF statement to either
put a 1 or a 0 depending on another sheet. I would like the button color to
be grey 25% if the if statement result is 0 and light green if the if
statement result is 1. Does anyone know how this could work?

Thanks!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Change color of Shape based on condition - Macro?

The following code will get you started. You'll have to change the RGB
colors to get the colors you want. Put this code in the ThisWorkbook code
module.

Private Sub Workbook_SheetCalculate(ByVal SH As Object)
Dim Rng As Range
Dim ShapeName As String
Dim SHP As Shape

'''''''''''''''''''''''''''''
' Change the shape name
' to your shape's name.
'''''''''''''''''''''''''''''
ShapeName = "Rectangle 1"
'''''''''''''''''''''''''''''
' Change this range to the
' appropriate sheet and cell.
'''''''''''''''''''''''''''''
Set Rng = ThisWorkbook.Worksheets("Sheet1").Range("F9")
Set SHP = Rng.Parent.Shapes(ShapeName)

If Rng.Value = 0 Then
SHP.Fill.ForeColor.RGB = RGB(255, 0, 0)
Else
SHP.Fill.ForeColor.RGB = RGB(0, 255, 0) ' green
End If

End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"WBTKbeezy" wrote in message
...
I need to have an auto shape, just a regular rectangle, change color based
on
a cell next to it. The cell next to it contains an IF statement to either
put a 1 or a 0 depending on another sheet. I would like the button color
to
be grey 25% if the if statement result is 0 and light green if the if
statement result is 1. Does anyone know how this could work?

Thanks!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Change color of Shape based on condition - Macro?

Chip -

This works perfectly! I have a follow up though... Is there an easier way
if I have 24 rectangles to change color based on 24 different cells to do a
code instead of copying code 24 times?

"Chip Pearson" wrote:

The following code will get you started. You'll have to change the RGB
colors to get the colors you want. Put this code in the ThisWorkbook code
module.

Private Sub Workbook_SheetCalculate(ByVal SH As Object)
Dim Rng As Range
Dim ShapeName As String
Dim SHP As Shape

'''''''''''''''''''''''''''''
' Change the shape name
' to your shape's name.
'''''''''''''''''''''''''''''
ShapeName = "Rectangle 1"
'''''''''''''''''''''''''''''
' Change this range to the
' appropriate sheet and cell.
'''''''''''''''''''''''''''''
Set Rng = ThisWorkbook.Worksheets("Sheet1").Range("F9")
Set SHP = Rng.Parent.Shapes(ShapeName)

If Rng.Value = 0 Then
SHP.Fill.ForeColor.RGB = RGB(255, 0, 0)
Else
SHP.Fill.ForeColor.RGB = RGB(0, 255, 0) ' green
End If

End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"WBTKbeezy" wrote in message
...
I need to have an auto shape, just a regular rectangle, change color based
on
a cell next to it. The cell next to it contains an IF statement to either
put a 1 or a 0 depending on another sheet. I would like the button color
to
be grey 25% if the if statement result is 0 and light green if the if
statement result is 1. Does anyone know how this could work?

Thanks!




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Change color of Shape based on condition - Macro?

IF the cell that controls the shape's color is the Top Left Cell of the
shape, you could use code like the following. As before, adjust the RGB
values to the colors you want.

Private Sub Workbook_SheetCalculate(ByVal SH As Object)
Dim SHP As Shape
Dim TLC As Range
For Each SHP In SH.Shapes
Set TLC = SHP.TopLeftCell
If TLC.Value = 0 Then
SHP.Fill.ForeColor.RGB = RGB(255, 0, 0) 'red
Else
SHP.Fill.ForeColor.RGB = RGB(0, 255, 0) 'green
End If
Next SHP
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"WBTKbeezy" wrote in message
...
Chip -

This works perfectly! I have a follow up though... Is there an easier way
if I have 24 rectangles to change color based on 24 different cells to do
a
code instead of copying code 24 times?

"Chip Pearson" wrote:

The following code will get you started. You'll have to change the RGB
colors to get the colors you want. Put this code in the ThisWorkbook code
module.

Private Sub Workbook_SheetCalculate(ByVal SH As Object)
Dim Rng As Range
Dim ShapeName As String
Dim SHP As Shape

'''''''''''''''''''''''''''''
' Change the shape name
' to your shape's name.
'''''''''''''''''''''''''''''
ShapeName = "Rectangle 1"
'''''''''''''''''''''''''''''
' Change this range to the
' appropriate sheet and cell.
'''''''''''''''''''''''''''''
Set Rng = ThisWorkbook.Worksheets("Sheet1").Range("F9")
Set SHP = Rng.Parent.Shapes(ShapeName)

If Rng.Value = 0 Then
SHP.Fill.ForeColor.RGB = RGB(255, 0, 0)
Else
SHP.Fill.ForeColor.RGB = RGB(0, 255, 0) ' green
End If

End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"WBTKbeezy" wrote in message
...
I need to have an auto shape, just a regular rectangle, change color
based
on
a cell next to it. The cell next to it contains an IF statement to
either
put a 1 or a 0 depending on another sheet. I would like the button
color
to
be grey 25% if the if statement result is 0 and light green if the if
statement result is 1. Does anyone know how this could work?

Thanks!






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change line chart color based on condition like + and -. egii Charts and Charting in Excel 1 May 8th 09 09:06 AM
how to change a cell color based on its content using macro? Ranger888 New Users to Excel 6 December 15th 08 08:23 PM
Change row color based on condition of celss B G Excel Worksheet Functions 3 June 28th 06 08:56 PM
change shape by condition Libramanuk Excel Discussion (Misc queries) 2 March 21st 06 02:51 PM
Color Change in chart based of condition KRT Charts and Charting in Excel 1 July 1st 05 12:51 PM


All times are GMT +1. The time now is 01:23 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"