根据x轴格式化数据透视图数据标签

我试图find一种方法(如果可能)将数据标签的文本颜色更改为红色,如果X轴恰好是某个标签。

我遇到的问题是我的图表是dynamic的(每天通过PowerPivot进行刷新,并有date和其他切片器来更改图表中显示的数据),所以即使将数据标签颜色设为红色,刷新图表时,它恢复到原来的黑色字体。

例如,在下面的图片中,我有一个图表跟踪他们已经打开的天数; 目标是在45天内解决所有票据; 因此,当图表显示“45+”部分中的任何数据时,我希望该数据标签的字体为红色。

我的图表示例:

我的图表示例:

在我的研究中,我能够find并修改一些使所有数据标签变成红色的代码,但是我使用VBA来处理图表是非常新的,并且希望得到一些帮助。 我到目前为止的代码是:

Sub ChrtTest() Dim i As Long Sheets("Dashboard").Select ActiveSheet.ChartObjects("DB_Chrt_1").Activate ActiveChart.PlotArea.Select With ActiveChart For i = 1 To .SeriesCollection.Count With .SeriesCollection(i).Format.Fill Select Case .Parent.Parent.Name Case "45+" .Visible = msoTrue With .Parent.Parent .ApplyDataLabels With .DataLabels .Position = xlLabelPositionOutsideEnd .Font.Color = vbYellow End With End With Case Else .Visible = msoTrue With .Parent.Parent .ApplyDataLabels With .DataLabels .Position = xlLabelPositionOutsideEnd .Font.Color = vbRed End With End With End Select End With Next i End With End Sub 

一些代码让你开始:

 Sub template() Dim pc As Chart Set pc = Sheets("Dashboard").ChartObjects("DB_Chrt_1").Chart Dim ax As Axis Set ax = pc.Axes(xlCategory) Dim dSet As Series Set dSet = pc.SeriesCollection(1) Dim dPoints As Points Set dPoints = dSet.Points pc.ApplyDataLabels For i = 1 To dPoints.Count If ax.CategoryNames(i) = "45+" Then With dPoints(i).DataLabel.Format.TextFrame2.TextRange.Characters .Font.Fill.ForeColor.RGB = RGB(255, 0, 0) End With End If Next i End Sub 

这应该将dataLabel文本设置为红色。 你也可以与其他属性混在一起