PC Review


Reply
Thread Tools Rate Thread

Change color of Shape based on condition - Macro?

 
 
=?Utf-8?B?V0JUS2JlZXp5?=
Guest
Posts: n/a
 
      20th Jan 2007
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 With Quote
 
 
 
 
Chip Pearson
Guest
Posts: n/a
 
      20th Jan 2007
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" <(E-Mail Removed)> wrote in message
news:C976E9D4-8D35-453B-88BD-(E-Mail Removed)...
>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 With Quote
 
=?Utf-8?B?V0JUS2JlZXp5?=
Guest
Posts: n/a
 
      20th Jan 2007
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" <(E-Mail Removed)> wrote in message
> news:C976E9D4-8D35-453B-88BD-(E-Mail Removed)...
> >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 With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      20th Jan 2007
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" <(E-Mail Removed)> wrote in message
news:B9F38F95-1277-4AEE-9B6A-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news:C976E9D4-8D35-453B-88BD-(E-Mail Removed)...
>> >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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change the row color of the Repeater based on some condition? Michael Microsoft ASP .NET 4 20th Jun 2008 07:29 AM
Change row color based on condition of celss =?Utf-8?B?QiBH?= Microsoft Excel Worksheet Functions 3 28th Jun 2006 08:56 PM
Change background color of shape through macro =?Utf-8?B?SmVubmlmZXI=?= Microsoft Powerpoint 9 25th May 2006 09:00 PM
Color Change in chart based of condition =?Utf-8?B?S1JU?= Microsoft Excel Charting 1 1st Jul 2005 12:51 PM
Change textbox color based on a condition T. W. Microsoft Access Forms 2 30th Dec 2004 03:59 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:41 PM.