读取图表标签值运行时错误(Excel VBA)
我正在为下表创建图表: 添加图表后,我希望根据标签值为图表栏上色,如果标签值为0,则标签应更改为“关闭” 下面是我执行此操作的代码:读取图表标签值运行时错误(Excel VBA),vba,excel,Vba,Excel,我正在为下表创建图表: 添加图表后,我希望根据标签值为图表栏上色,如果标签值为0,则标签应更改为“关闭” 下面是我执行此操作的代码: Dim ChartRng As Range Set ChartRng = Worksheets("Overview").Range("A1:C19") Dim oChtObj As ChartObject Set oChtObj = Worksheets("Overview").ChartObjec
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
Dim ChartRng As范围
Set ChartRng=工作表(“概述”).范围(“A1:C19”)
Dim oChtObj作为图表对象
设置oChtObj=Worksheets(“Overview”).ChartObjects.Add(左:=48,宽:=570,顶:=1000,高:=367)
用oChtObj.图表
.Parent.Name=“性能”
.ChartType=xlColumnClustered
.ApplyLayout(1)
.SetSourceData图表
.HasLegend=True
.SeriesCollection(1).HasDataLabels=True
.SeriesCollection(2).HasDataLabels=False
.hasttle=正确
.ChartTitle.Caption=“每个代理面临呼叫的时间(KPI:75%)
.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).删除
.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(217217217217)
.Axes(xlValue).MajorGridlines.Border.Color=RGB(217217217217217)
以
设置oChtObj=Nothing
将oPoint设置为Excel.Point
单件尺寸SNG百分比
对于工作表中的每个点(“概述”).ChartObjects(“性能”).Chart.SeriesCollection(1).点
sngPercente=CSng(拆分(oPoint.DataLabel.Caption,“%”)(0))
带点
如果sngPercente<70,则
.Interior.Color=RGB(255,0,0)
如果结束
如果sngPercente>75,则
.Interior.Color=RGB(0,176,80)
如果结束
如果sngPercente>=70且sngPercente我不确定Excel2007是否有属性,因为我无法测试它
试试这个
添加这一行
工作表(“概述”).ChartObjects(“性能”).Chart.SeriesCollection(1).HasDataLabels=True
在工作表中每个操作点的(“概述”)之前…
循环,现在尝试一下
如果它仍然不起作用,那么我将删除这篇文章
编辑
根据本协议,该财产存在于Office 2007中
在Teamviewer上的进一步测试表明,在此版本中,在读取数据标签的值之前,必须先选择数据标签。所以我们所要做的就是添加
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
Dim vYVals作为变量
Dim srs As系列
只要
将dPctg调暗为双色“单色优于双色”
设置srs=ActiveChart.SeriesCollection(1)
vYVals=srs.值
对于iPt=1至srs.Points.Count
dPctg=vYVals(iPt)
带srs点(iPt)
选择案例dPctg
案例0
.DataLabel.Caption=“关闭”
病例<0.7
.Interior.Color=RGB(255,0,0)
案例>0.75
.Interior.Color=RGB(0,176,80)
其他情况
.Interior.Color=RGB(148、208、80)
结束选择
以
下一个
即使按照您的建议添加了行,仍然会出现相同的错误。也许我需要加载某个库,或者我缺少其他东西?我没有Excel 2007。但是如果你有teamviewer的话我可以帮你查一下。。。我不确定teamview,因为我正在公司计算机上运行它,可能不允许我安装它。如果有帮助的话,这里有一份excel文件的副本:让我看看我是否可以在一台有excel 2007的朋友电脑上测试它。谢谢。那么,有没有其他方法可以让我尝试这样做呢。可能是不同的语法或函数?