vba - 在vba中为图例设置颜色代码

标签 vba excel charts pivot-table

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

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

enter image description here

最佳答案

这可以通过操作 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)

关于vba - 在vba中为图例设置颜色代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34153465/

相关文章:

c# - Codeplex Excel 数据阅读器为 Excel 2010 提供空数据集

javascript - 浏览器中的 Google Chart 高 RAM

vba - 使用 Excel 宏中的 Word Selection 对象

excel - 使用 VBA 更改 Internet Explorer 编码

excel - 比引用单元格更有效的替代方法?

php - Laravel-Excel (MaatWebSite) 需要长时间阅读

vba - 在我自己的 vba 函数中获得没有 gosub 的返回

excel在表格中查找索引,在原点中搜索任何数字并在目标表格中匹配

charts - Highcharts,如何根据值更改饼图中的可数据距离

javascript - 是否可以在垂直谷歌聊天线图上包含与条形内嵌的单独条形标签?