PDA

View Full Version : [SOLVED:] Button click to change shape color



Ryanchris
12-15-2019, 01:41 AM
Hi all!
I have what I believe is a simple question but as I am very new to VBA, i can't figure out how to set up a button on one sheet that changes the color of a shape on another sheet.
I have a square drawn on sheet2 named Squarebutton. I have an oval on sheet1 that is currently blue. I'd like it to change to orange when Squarebutton is pressed. Any help would be so appreciated!!
Thank you!

paulked
12-15-2019, 07:05 AM
Record a Macro and pick the bones out of it:

Paul_Hossler
12-15-2019, 08:34 AM
Record a Macro and pick the bones out of it:


:thumb Best way to learn

Ryanchris
12-15-2019, 06:30 PM
When I tried the record macro method, I got a very different formula than the one you provided and just couldn't get it to work. I'd like to use yours as it toggles between the colors. I don't seem to understand the logic of it though. Could you break it down for me? I keep reading it as though it says "if oval 1 is orange its blue, else its blue". Apologize, I am just so new to this and frustrated. I need to do the same thing with changing the font color in a textbox next and thinking if I can understand this, maybe I can figure that out also. Thank you for your help and understanding.

Paul_Hossler
12-15-2019, 07:41 PM
Butting in ... I added some comments. See if they help

Put your cursor on a VBA word and hit F1 to get help




Option Explicit


'this macro is assigned to SquareButton on Sheet1
Sub KedTest()



'if Oval 1 on Sheet1 has a Fill Foreground color = BLUE then ....
If Sheet1.Shapes("Oval 1").Fill.ForeColor.RGB = RGB(255, 192, 0) Then

'make Oval 1 on Sheet1's Fill Foreground color = ORANGE
Sheet1.Shapes("Oval 1").Fill.ForeColor.RGB = RGB(0, 112, 192)

'otherwise
Else
'make Oval 1 on Sheet1's Fill Foreground color = ORANGE
Sheet1.Shapes("Oval 1").Fill.ForeColor.RGB = RGB(255, 192, 0)
End If
End Sub




'more concise
Sub KedTestNew()

With Sheet1.Shapes("Oval 1").Fill.ForeColor
If .RGB = RGB(255, 192, 0) Then
.RGB = RGB(0, 112, 192)
Else
.RGB = RGB(255, 192, 0)
End If
End With
End Sub


'even more concise
Sub KedTestNew2()

With Sheet1.Shapes("Oval 1").Fill.ForeColor
.RGB = IIf(.RGB = RGB(255, 192, 0), RGB(0, 112, 192), RGB(255, 192, 0))
End With
End Sub

Ryanchris
12-15-2019, 08:06 PM
You guys are great. I totally understand now. Thank you thank you thank you!
I have to do the same thing with text in a textbox.

Here is the code I am trying to use:

Worksheets("Designs").Shapes.Range(Array("Textbox 6")).Select
Selection.ShapeRange.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0)

This works and it does change the font color.
Is there anyway to have the color toggle like it does in the code you just gave me? Also, the text stays selected after it changes color. Is there any way to get rid of those marks that indicate the text is selected?

Paul_Hossler
12-15-2019, 08:57 PM
Maybe - not tested - something like this




Option Explicit


Sub ToggleTextboxFont()


Worksheets("Designs").Shapes.Range(Array("Textbox 6")).Select
With Selection.ShapeRange.TextFrame2.TextRange.Font.Fill.ForeColor
If .RGB = RGB(255, 0, 0) Then
.RGB = RGB(0, 0, 255)
Else
.RGB = RGB(255, 0, 0)
End If
End With
End Sub

Ryanchris
12-15-2019, 11:14 PM
Paul - that code allows me to toggle between colors perfectly! However, the marks indicating that the Textbox is selected are showing and my concern is that a user may change the text inside that box accidentally - not knowing that its highlighted...25638

paulked
12-16-2019, 12:02 AM
Thanks Paul, I was sleeping :thumb

Ryanchris, to avoid the selection try:



Sub ToggleTextboxFont()




With Worksheets("Designs").Shapes("Textbox 6").TextFrame2.TextRange.Font.Fill.ForeColor
If .RGB = RGB(255, 0, 0) Then
.RGB = RGB(0, 0, 255)
Else
.RGB = RGB(255, 0, 0)
End If
End With
End Sub

Ryanchris
12-19-2019, 03:25 PM
Thank you everyone! Worked beautifully.

paulked
12-19-2019, 03:31 PM
:thumb