简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM