Consulting

Results 1 to 11 of 11

Thread: Button click to change shape color

  1. #1

    Button click to change shape color

    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!

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Record a Macro and pick the bones out of it:
    Attached Files Attached Files
    Semper in excretia sumus; solum profundum variat.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Quote Originally Posted by paulked View Post
    Record a Macro and pick the bones out of it:

    Best way to learn
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    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.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    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?

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    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...Select.JPG

  9. #9
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Thanks Paul, I was sleeping

    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
    Semper in excretia sumus; solum profundum variat.

  10. #10
    Thank you everyone! Worked beautifully.

  11. #11
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Semper in excretia sumus; solum profundum variat.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •