VBA根据包含一些文本的标题更新图表
我有一张图表可以帮助我按照人物来计划一系列事件。 我需要一些事件来保持一致性。
例如,简在她的职业生涯中被聘用了两次。 我希望这个雇佣类别是一样的。 但是,Excel会将其作为不同的类别打断,因为其中一个名称为01-Hire,另一个为02-Hire。 在下面的例子中,所有雇员都应该是蓝色的。
我想要一些代码在页眉中search“Hire”,然后应用一致的颜色。 请注意 ,序列之间可能会有不同的标题,因此代码需要足够聪明,以便将包含相同文本(而不是序列号)的内容分组。
我能find的最接近的东西是在这里: 设置颜色代码的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 – 雇用01 – 晋升01 – 第二期 – 雇用02 – 晋升02 – 第三期 – 雇用03 – 晋升03 – 期限简38 10 29
本15 50 10乔68 56 10 7
丽莎61 41
珍妮24
杰里81 16
如果你的系列标签总是重复“雇佣x”,“舞台x”,“条款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