Change cell fill color based on font color

孤人 提交于 2019-12-12 06:02:06

问题


I need to move conditionally formatted data from Excel 2013 into pre-existing tables in PowerPoint 2013. The font colors and formatting will carry from Excel to PowerPoint, but the cell fill needs to be manually added.

Is it possible to create a macro in PowerPoint that will search through each table's cell, find one of five specific font colors "(xxx,xxx,xxx)", then fill that cell with a specified color?

I have tables in Excel that have conditional formatting colors with the following rules:

  • "Dark Green "
    Fill: (146, 208, 80) Font color: (79, 98, 40)

  • "Light Green"
    Fill: (195, 214, 155) Font color: (80, 98, 40)

  • "Grey"
    Fill: (242, 242, 242) Font color: (166, 166, 166)

  • "Light Pink"
    Fill: (230, 185, 184) Font color: (150, 55, 53)

  • "Dark Pink"
    Fill: (217, 150, 148) Font color: (149, 55, 53)

One way I can get the cell font and fill to stay is by creating a new chart, but that gets tedious when it needs to be done nearly a hundred times.

Ideally, I would like the macro to search through a presentation, if it finds a table cell value's font as (Dark green) (79, 98, 40), fill that cell to (149, 208, 80). Then continue searching for the next four colors as well.


回答1:


Option Explicit

Sub Tester()

    Dim s As Slide, p As Presentation, shp As Shape
    Dim rw As Row, cl As Cell

    For Each s In ActivePresentation.Slides
        For Each shp In s.Shapes
            If shp.HasTable Then
                For Each rw In shp.Table.Rows
                For Each cl In rw.Cells
                    ProcessCellColors cl
                Next cl
                Next rw
            End If
        Next shp
    Next s

End Sub

Sub ProcessCellColors(c As Cell)
    Dim tf As TextFrame, clr As Long
    Set tf = c.Shape.TextFrame
    clr = -1
    If tf.HasText Then
        'assumes all text has the same color...
        Select Case tf.TextRange.Font.Color.RGB
            Case vbBlack: clr = vbYellow 'my testing
            Case RGB(79, 98, 40): clr = RGB(146, 208, 80)
            Case RGB(80, 98, 40): clr = RGB(195, 214, 155)
            '....etc etc
        End Select
        If clr <> -1 Then
            c.Shape.Fill.ForeColor.RGB = clr
        End If
    End If
End Sub


来源:https://stackoverflow.com/questions/33575369/change-cell-fill-color-based-on-font-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!