Excel VBA操作图表数据标签的一个问题
Private Sub Workbook_Open()
On Error Resume Next
Sheet1.ChartObjects(1).Activate
Dim c%
c = ActiveChart.SeriesCollection.Count
For i = 1 To c
pc = ActiveChart.SeriesCollection(i).Points.Count
For j = 1 To pc
r = Round(Sheet1.Cells(i + 1, 8 + j) * 100, 2)
set p=ActiveChart.SeriesCollection(i).Points(j)
p.DataLabel.Format.TextFrame2.TextRange.Font.Size = 12
p.DataLabel.Format.TextFrame2.TextRange.Font.Bold = msoTrue
If (i = c) Then 'total
p.DataLabel.Formula= ActiveChart.SeriesCollection(i).Points(j).DataLabel.Text & "," & IIf(r > 0, " +" & Format(CStr(r), "0.##"), " " & Format(CStr(r), "0.##")) & "%"
p.DataLabel.Position = xlLabelPositionAbove
findex = InStr(p.DataLabel.Formula, ",") 'the index of ","
lens = Len(p.DataLabel.Formula) - findex 'the len from the index of "," to the end of the text
p.DataLabel.Format.TextFrame2.TextRange.Characters(1, findex - 1).Font.Fill.ForeColor.RGB = RGB(0, 0, 0)
If (r >= 0) Then
p.DataLabel.Format.TextFrame2.TextRange.Characters(findex + 1, lens).Font.Fill.ForeColor.RGB = RGB(0, 128, 0)
Else
p.DataLabel.Format.TextFrame2.TextRange.Characters(findex + 1, lens).Font.Fill.ForeColor.RGB = RGB(255, 0, 0)
End If
End If
Next
Next
End Sub
以上VBA语句只是将Excel模板图表中各个point的数据标签中加上一些额外的文本,并根据值的正负来赋予不同的颜色,但是,为什么执行后数据标签无法删除,并且,每次重新打开Excel模板,数据标签里依旧会保留上一次的结果?比如说,应该是 4,+1.2% 而不应该是 4,+1.2%,+1.2% VBA excel 数据标签
[解决办法]
同样的问题,同样的版块,你问了两遍,有毛病啊?我不是都告诉你了?分都给我!