View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Button Color based on cell

Afraid I was a bit cryptic last time, but you would not have formatted a
Forms button via Design mode / Properties as you said you did. Anyway the
name strongly implies it is an ActiveX CommandButton.

Changing the colour of the button is simple enough but the potential problem
in Excel 2007 is to determine the apparent cell colour. If in a Table it's
difficult (but not impossible), but if the colour is applied by one of the
new Conditional Formats it's extremely difficult and way beyond what's
viable to demo (I have yet to attempt it).

The following should work if the colour was been applied via the palette or
a Style. If the cell colour returns white it's far from conclusive as to
what the apparent colour is, so perhaps better to reset to its default, but
that's up to you. A CF colour will override even a formatted colour hence
following would be inadequate.

Sub test()
Dim nClr As Long
Dim rCel As Range
Dim ole As OLEObject

Set rCel = ActiveCell
' or say
Set rCel = ActiveSheet.Range("A1")

Set ole = ActiveSheet.OLEObjects("CommandButton11")

nClr = rCel.Interior.Color

' reset to default if black or white
If nClr = vbBlack Or nClr = vbWhite Then
nClr = vbButtonFace
End If

ole.Object.BackColor = nClr

End Sub

Obviously change the cell-ref and button name to suit.

Now the second problem!
The apparent colour can change for all sorts of reasons, some of these can
be worked out in an appropriate worksheet event. However if the colour has
been manually applied as a format nothing is going to trigger the code to
change the button colour, you'll need to run run something like the above
macro.

Regards,
Peter T



"Thanks" wrote in message
...
Sorry it is CommandButton11

"Peter T" wrote:

No you didn't do that. What's its name as I asked before (assuming you
didn't change it after it was created).

Regards,
Peter T

"Thanks" wrote in message
...
Added from developer tool bar Inset Button (Form Control) To change
color I
click Design Mode then right click the button Properties and then
change
Back Color in properties list.



"Peter T" wrote:

Which toolbar did you get it from.
what's its name.
How do you manually change the colour.

Regards,
Peter T

"Thanks" wrote in message
...
I suppose it is a COmmandButton because I can manually change the
color
of
the button. So now what?
Thanks for your response.

"Peter T" wrote:

You can't change the colour of a Forms button though you can change
the
font. (I assume you don't mean a CommandButton).

You can make a 'Bevel' Autoshape look like a button with a little
customizing and removing its lines lines. Add some text to it and
assign
a
macro

Assuming your Bevel is named ("AutoShape 1") try the following

Sub test()
Dim idx As Long
Dim cel As Range
Dim shp As Shape

Set cel = Range("A1")
idx = Range("A1").Interior.ColorIndex
If idx < 1 Then idx = 58

Set shp = ActiveSheet.Shapes("AutoShape 1")
shp.Fill.ForeColor.SchemeColor = idx + 7

End Sub

Above asssume Excel 2003 or earlier. In Excel 2007 would need to
get
the
cell's RGB colour and change Schemecolor to .RGB

Regards,
Peter T

PS, the bevel looks like a double rectangle


"Thanks" wrote in message
...
Hello
How can I change the button color based on cell contents or fill
color.
I
have button one sheet and I would like for it to mimic the color
of
a
cell
in
another sheet. The cell content is either "Red", "Yellow",
"Green".