locked
Changing colors of shape to green, red, yellow on click RRS feed

  • Question

  • I have created shape and try to change the colors from red, green, yello by clicking the shape.

    I got certain logic,  but could not reach finall I wanted to do.

    In fact, there are three modules like herebelow,  can you please give me some support on this issue.

    Those all threes are not working.

     

     

    one module

     

    Sub trafficlight_cLICK()
    With ActiveSheet.Shapes
    Selection.ShapeRange.Fill.ForeColor.SchemeColor = 2
    End With
    End Sub

    Sub trafficlight2_click()
    With ActiveSheet.Shapes
    Selection.ShapeRange.Fill.ForeColor.SchemeColor = 5
    End With
    End Sub
    Sub trafficlight3_click()
    With ActiveSheet.Shapes
    Selection.ShapeRange.Fill.ForeColor.SchemeColor = 11
    End With
    End Sub

     

     

    2nd module

     

    Private Sub CommandButton1_Click()
    Dim col As Integer, c As Integer
    col = Selection.ShapeRange.Fill.ForeColor.SchemeColor
    For c = 1 To 30
        col = IIf(col = 30, 1, col)
            If col = c Then
              Selection.ShapeRange.Fill.ForeColor.SchemeColor = col + 1
            End If
     Next c
    End Sub

     

     

    3rd

     

    Sub traffic_cLICK()

    With ActiveSheet.Shapes

      Selection.ShapeRange.Fill.ForeColor.SchemeColor = 11

      Selection.ShapeRange.Fill.ForeColor.SchemeColor = 5


      Selection.ShapeRange.Fill.ForeColor.SchemeColor = 2

    End With

    End Sub

     

     

    Thank you.

    Thursday, April 18, 2013 1:14 PM

Answers

  • i think you mean that you want to have a logical sequence from green to yellow to red to green ...

    so you could make a userform with 1 commandbutton

    Option Explicit Private col As Long Private Sub CommandButton1_Click() ActiveDocument.Shapes("Oval 3").Select col = Selection.ShapeRange.Fill.ForeColor.RGB Select Case col Case vbGreen col = vbYellow Case vbYellow col = vbRed Case vbRed col = vbGreen Case Else col = vbGreen End Select Selection.ShapeRange.Fill.ForeColor.RGB = col End Sub

    • Proposed as answer by Shasur Sunday, April 21, 2013 2:41 AM
    • Marked as answer by Damon Zheng Thursday, July 4, 2013 1:22 PM
    Friday, April 19, 2013 8:49 PM

All replies

  • See the replies to your identical question in the Microsoft Community forums: Changing color of shape to red, green, yellow on click in VBA.

    Regards, Hans Vogelaar

    Thursday, April 18, 2013 3:12 PM
  • why do you make it that difficult?

    but: in which program are you working? powerpoint, excel or word ( i expect powerpoint, otherwise it is pretty unuseful)

    to make it clear: the code to change a color of a shape is: 

        ActiveDocument.Shapes("Rectangle 2").Select
        Selection.ShapeRange.Fill.ForeColor.RGB = vbBlack
    

    you could of course use vbYellow, vbGreen, vbRed or just give the RGB code


    Thursday, April 18, 2013 7:10 PM
  • thank you for your reply.

    but  on click , I need to make loop to choose yellow, green,red..

    Thursday, April 18, 2013 8:03 PM
  • i think you mean that you want to have a logical sequence from green to yellow to red to green ...

    so you could make a userform with 1 commandbutton

    Option Explicit Private col As Long Private Sub CommandButton1_Click() ActiveDocument.Shapes("Oval 3").Select col = Selection.ShapeRange.Fill.ForeColor.RGB Select Case col Case vbGreen col = vbYellow Case vbYellow col = vbRed Case vbRed col = vbGreen Case Else col = vbGreen End Select Selection.ShapeRange.Fill.ForeColor.RGB = col End Sub

    • Proposed as answer by Shasur Sunday, April 21, 2013 2:41 AM
    • Marked as answer by Damon Zheng Thursday, July 4, 2013 1:22 PM
    Friday, April 19, 2013 8:49 PM