【问题标题】:Set Color codes to the legends in vba将颜色代码设置为 vba 中的图例
【发布时间】:2016-03-13 05:31:19
【问题描述】:

我在每个工作表中都有数据透视表,我必须对它们进行比较,但是每个工作表中图例的颜色不同。 如何设置颜色?

例如,如果我的图例条目是“ISO”,我希望它始终是“蓝色”,如果它的“LAT”我希望它在每张纸上都是“红色”。

【问题讨论】:

    标签: vba excel charts pivot-table


    【解决方案1】:

    这可以通过操作ChartSeriesCollection 属性中的Series 对象来完成。

    我在下面编写了一个简短的示例方法,它采用工作表、图例名称和目标 RGB 颜色。它循环工作表的形状,如果它们包含图表,则查找具有指定legendNameSeries。如果合适,它会将前景色更改为指定的 RGB 颜色。

    Private Sub FormatShapeLegend(sheet As Worksheet, legendName As String, targetColor As MsoRGBType)
        Dim shp As Shape
        Dim chrt As Chart
        Dim s As Series
    
        For Each shp In sheet.Shapes
            If shp.HasChart Then
                Set chrt = shp.Chart
    
                'Loop the dataseries to find the legend with the desired name.
                For Each s In chrt.SeriesCollection
                    'If the name fits, go ahead and format the series.
                    If LCase(s.Name) = LCase(legendName) Then
                        s.Format.Fill.ForeColor.RGB = targetColor
                    End If
                Next
            End If
        Next
    End Sub
    

    示例用法:

    FormatShapeLegend ActiveSheet, "ISO", RGB(0, 0, 255)
    

    【讨论】:

      猜你喜欢
      • 2017-03-28
      • 1970-01-01
      • 2019-09-07
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 2013-10-06
      相关资源
      最近更新 更多