Change color of autoshape in vba

bhlabelle
bhlabelle used Ask the Experts™
on
I have a spreadsheet with a few autoshapes, and I want the user to be able to change the color in one cell, press a button to run my code, and have all the autoshapes change to the color in the cell.  I wanted to try this for one autoshape to begin with, but I'm running into a weird problem.

I have the following code

Dim Color as Integer
Color = Sheets("My Sheet").Cells(1,"A").Interior.ColorIndex
ActiveSheet.Shapes("AutoShape 2").Fill.ForeColor.SchemeColor = Color

So if I change the color in Cell 1, A to red, I get the corret ColorIndex set as my integer, however, it does not change the autoshape color to red.  It seems to blend the red with the previous color.  While this might not be what is actually happening, I can tell you that the color is changed, but it is not to red.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Well, I found a solution.  Not sure if this is the best way, but it seems to work.  Basically I call three separate functions to get the R, G, B values, and then change the .RGB value of the autoshape.

color1 = showRGB1(Cells(1, "A"))
color2 = showRGB2(Cells(1, "A"))
color3 = showRGB3(Cells(1, "A"))
ActiveSheet.Shapes("AutoShape 2").Fill.ForeColor.RGB = RGB(color1, color2, color3)


Function showRGB1(rcell)
Dim myStr As String
Dim myInt As Integer
Application.Volatile
myStr = Right("000000" & Hex(rcell.Interior.color), 6)
myInt = Application.Evaluate("=Hex2dec(""" & Right(myStr, 2) & """)")
showRGB1 = myInt

End Function

Function showRGB2(rcell)
Dim myStr As String
Dim myInt As Integer
Application.Volatile
myStr = Right("000000" & Hex(rcell.Interior.color), 6)
myInt = Application.Evaluate("=Hex2dec(""" & Mid(myStr, 3, 2) & """)")
showRGB2 = myInt

End Function


Function showRGB3(rcell)
Dim myStr As String
Dim myInt As Integer
Application.Volatile
myStr = Right("000000" & Hex(rcell.Interior.color), 6)
myInt = Application.Evaluate("=Hex2dec(""" & Left(myStr, 2) & """)")
showRGB3 = myInt

End Function

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial