Need Help Changing the Color of an Object in Sheet1, from a cell in Sheet2 using VBA!

Spartan920

New Member
Joined
Sep 14, 2012
Messages
9
Before I get started, I know this is a tall order...haha. I also know that this type of question has been answered and revisited time and time again. I found a lot of useful posts, but would really like something specific to what I'm doing.

I have an Oval (Oval 1) in Sheet1, and I'd like to change it's color from cell A1 in Sheet2 using VBA.
The colors are: Red- (255, 0, 0)
Yellow- (255, 255, 109)
Green- (41, 247, 46)
and Grey- (127, 127, 127)

I'd like these colors to Fill the Oval object. I'm not too worried about the line color at this point.

Can anyone help with the coding? I'm lost.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this...

Code:
[color=darkblue]Sub[/color] Oval1_Color()
    [color=darkblue]With[/color] Sheets("Sheet1").Shapes("Oval 1").Fill.ForeColor
        [color=darkblue]Select[/color] [color=darkblue]Case[/color] U[color=darkblue]Case[/color](Sheets("Sheet2").Range("A1").Value)
            [color=darkblue]Case[/color] "RED": .RGB = vbRed
            [color=darkblue]Case[/color] "YELLOW": .RGB = vbYellow
            [color=darkblue]Case[/color] "GREEN": .RGB = RGB(41, 247, 46)
            Case "GREY": .RGB = RGB(127, 127, 127)
        [color=darkblue]End[/color] [color=darkblue]Select[/color]
    [color=darkblue]End[/color] [color=darkblue]With[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
I think you're on to something AlphaFrog. This is the first time I entered a code and didn't get an error message. But when I went to Sheet2/Cell A1 to enter one of the designated colors, the oval on sheet1 didn't change colors.

Any ideas?
 
Upvote 0
Did you run the macro after you entered a color in A1?

Did you expect it to "automatically" change colors after you enter a color? That wasn't part of your description.
 
Upvote 0
Sorry about that AlphaFrog. Once again it was user error, haha. Your code worked to perfection. I didn't enter the color first and run macro afterwards. Not to take up too much of your time, but is there in fact a way to do it automatically like you mentioned in your previous post?

If not, the code you provided is just fine. Thanks again.
 
Upvote 0
Right-click on the Sheet2 tab
Select View Code from the pop-up context menu
Paste the code below in the worksheet's code module

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] Worksheet_Change([COLOR=darkblue]ByVal[/COLOR] Target [COLOR=darkblue]As[/COLOR] Range)
    [COLOR=darkblue]If[/COLOR] Target.Address(0, 0) = "A1" [COLOR=darkblue]Then[/COLOR]
        [COLOR=darkblue]With[/COLOR] Sheets("Sheet1").Shapes("Oval 1").Fill.ForeColor
            [COLOR=darkblue]Select[/COLOR] [COLOR=darkblue]Case[/COLOR] U[COLOR=darkblue]Case[/COLOR](Target.Value)
                [COLOR=darkblue]Case[/COLOR] "RED": .RGB = vbRed
                [COLOR=darkblue]Case[/COLOR] "YELLOW": .RGB = vbYellow
                [COLOR=darkblue]Case[/COLOR] "GREEN": .RGB = RGB(41, 247, 46)
                Case "GREY": .RGB = RGB(127, 127, 127)
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Select[/COLOR]
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
End [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,223,920
Messages
6,175,374
Members
452,638
Latest member
Oluwabukunmi

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