在VBA中为图例设置颜色代码

3
我在每个工作表中都有数据透视表,需要比较它们,但是每个工作表中图例的颜色不同。如何设置颜色?
例如,如果我的图例条目是“ISO”,我希望它始终为“蓝色”,如果是“LAT”,则在每个工作表中都为“红色”。

enter image description here

1个回答

1
这可以通过操作图表中的SeriesCollection属性中的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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接