0

我有一张图表,可以帮助我按人绘制一系列事件。当我绘制图表时,我需要一些类别的事件来保持一致性。

例如,简在她的职业生涯中被聘用了两次。我希望那个级别的雇佣是一样的。但是,Excel 会将这些作为不同的类打断,因为一个名为 01-Hire,另一个名为 02-Hire。在下面的示例中,所有员工都应该是蓝色的。

我想要一些代码在标题中搜索“雇用”,然后应用一致的颜色。请注意,序列之间可能存在不同的标题,因此代码需要足够智能,以便仅对包含相同文本(而不是序列号)的事物进行分组。

我能找到的最接近的方法是: 将颜色代码设置为 vba 中的图例

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)

我想对下面的所有类执行此操作,类似于图表。

期望的输出 在此处输入图像描述

数据表 在此处输入图像描述

原始代码 行标签 01 - Hire 01 - Promotion 01 - Term 02 - Hire 02 - Promotion 02 - Term 03 - Hire 03 - Promotion 03 - Term Jane 38 10 29
Ben 15 50 10 Joe 68 56 10 7
Lisa 61 41
Jenny 24
杰瑞 81 16

4

1 回答 1

1

如果您的系列标签总是重复“Hire x”、“Prom x”、“Term x”,那么这样的事情会起作用:

Dim s As Series, x As Long
x = 0

For Each s In ActiveSheet.ChartObjects(1).Chart.SeriesCollection
    x = x + 1
    s.Format.Fill.ForeColor.RGB = Array(vbBlue, vbRed, vbGreen)(x Mod 3)
Next s

如果您需要根据系列名称执行此操作,则:

Dim s As Series, clr As Long, nm As String

For Each s In ActiveSheet.ChartObjects(1).Chart.SeriesCollection

    nm = LCase(s.Name)

    clr = vbYellow 'default
    If nm Like "*hire*" Then
        clr = vbBlue
    ElseIf nm Like "*prom*" Then
        clr = vbGreen
    ElseIf nm Like "*term*" Then
        clr = vbRed
    End If

    s.Format.Fill.ForeColor.RGB = clr

Next s
于 2016-09-15T21:08:06.690 回答