vbapowerpoint

Macro to update shape color on click in PowerPoint


I am trying to create a macro that will toggle a shape's color upon clicking. I've assigned the macro below to a shape, and it should execute with a click. However, when I click, nothing happens. Do I need to name the shape something specific? or is there something wrong in the VB language?

Sub ChangeShapeColour(ByRef oShp As Shape)
    Dim oSh As Shape
    ' IF the shape's .Fill.ForeColor.RGB = red color:
        If oSh.Fill.ForeColor.RGB = RGB(199, 70, 52) Then
    ' Change it to green:
        oSh.Fill.ForeColor.RGB = RGB(117, 156, 108)

        End If
        
    ' IF the shape's .Fill.ForeColor.RGB = green color:
        If oSh.Fill.ForeColor.RGB = RGB(117, 156, 108) Then
    ' Change it to red:
        oSh.Fill.ForeColor.RGB = RGB(199, 70, 52)
        
        End If


End Sub

Solution

  • Toggle Shape Color

    The Issue

    Sub ToggleShapeColorYourCode(shp As Shape)
        
        Dim Red As Long: Red = RGB(199, 70, 52) ' some red color
        Dim Green As Long: Green = RGB(117, 156, 108) ' some green color
        
        With shp.Fill.ForeColor
            
            If .RGB = Red Then .RGB = Green
            If .RGB = Green Then .RGB = Red
            
            ' With the previous two 'If' statements you are implying the following:
            
            If .RGB <> Red And .RGB <> Green Then ' it's neither
                ' do nothing!?
            End If
        
        End With
    
    End Sub
    

    The Fix

    1.) If Long

    Sub ToggleShapeColorIFLong(shp As Shape)
        
        Dim Red As Long: Red = RGB(199, 70, 52) ' some red color
        Dim Green As Long: Green = RGB(117, 156, 108) ' some green color
        
        With shp.Fill.ForeColor
            If .RGB = Red Then
                .RGB = Green
            ElseIf .RGB = Green Then
                .RGB = Red
            Else ' it is neither
                .RGB = Red ' or Green, your choice
            End If
        End With
    
    End Sub
    

    2.) If Short

    Sub ToggleShapeColorIfShort(shp As Shape)
        
        Dim Red As Long: Red = RGB(199, 70, 52) ' some red color
        Dim Green As Long: Green = RGB(117, 156, 108) ' some green color
        
        With shp.Fill.ForeColor
            If .RGB = Red Then
                .RGB = Green
            Else ' it is not red
                .RGB = Red
            End If
        End With
    
    End Sub
    

    3.) IF Shorter

    Sub ToggleShapeColorIfShort(shp As Shape)
        
        Dim Red As Long: Red = RGB(199, 70, 52) ' some red color
        Dim Green As Long: Green = RGB(117, 156, 108) ' some green color
        
        With shp.Fill.ForeColor
            If .RGB = Red Then .RGB = Green Else .RGB = Red
        End With
    
    End Sub
    

    4.) IIF

    Sub ChangeShapeColorIIf(ByVal shp As Shape)
        
        Dim Red As Long: Red = RGB(199, 70, 52)
        Dim Green As Long: Green = RGB(117, 156, 108)
        
        With shp.Fill.ForeColor
            .RGB = IIf(.RGB = Red, Green, Red)
        End With
    
    End Sub
    

    5.) Select Long

    Sub ToggleShapeColorCaseElse(shp As Shape)
        
        Dim Red As Long: Red = RGB(199, 70, 52) ' some red color
        Dim Green As Long: Green = RGB(117, 156, 108) ' some green color
        
        With shp.Fill.ForeColor
            Select Case .RGB
                Case Red: .RGB = Green
                Case Green: .RGB = Red
                Case Else: .RGB = Red ' or Green, your choice
            End Select
        End With
    
    End Sub
    

    6.) Select Short

    Sub ToggleShapeColorCase(shp As Shape)
        
        Dim Red As Long: Red = RGB(199, 70, 52) ' some red color
        Dim Green As Long: Green = RGB(117, 156, 108) ' some green color
        
        With shp.Fill.ForeColor
            Select Case .RGB
                Case Red: .RGB = Green
                Case Else: .RGB = Red
            End Select
        End With
    
    End Sub