Home > other >  Change the color of 1 cell in every table with Powerpoint VBA
Change the color of 1 cell in every table with Powerpoint VBA

Time:05-03

I'm trying to change the color of 1 cell in every table with Powerpoint VBA

Right now my code colors every cell in the 1st row of every table. How do I make it color just the cell in row 1 column 2?

Sub RecolorTableHeader()
    Dim oSl As Slide
    Dim oSh As Shape
    Dim x As Long

    For Each oSl In ActivePresentation.Slides
        For Each oSh In oSl.Shapes
            If oSh.HasTable Then
                With oSh.Table
                    For x = 1 To .Columns.Count
                        .Cell(1, x).Shape.Fill.ForeColor.RGB = RGB(255, 0, 0)
                    Next
                End With
            End If
        Next
    Next
End Sub

CodePudding user response:

From the comments above, it looks like you were close, but some of your tables did not have a cell at position 1,2 and this was causing your error.

Consider trying the below snippet:

Sub RecolorTableHeader()
    Dim oSl As Slide, _
        oSh As Shape

    For Each oSl In ActivePresentation.Slides
        For Each oSh In oSl.Shapes
            If oSh.HasTable Then
                With oSh.Table
                    If .Columns.Count >= 2 Then
                        Let .Cell(1, 2).Shape.Fill.ForeColor.RGB = rgbRed
                    End If
                End With
            End If
    Next oSh, oSl
End Sub
  • Related