问题
I am trying to figure out an efficient way, either VBA or not, to format the data labels on a line graph I have.
The graph should present the series name at the right most data label, for each series, while the remaining data labels are blank.
Currently I just add data labels through each series, and then format each individual data label separately - but this is slow when I have series with multiple points, in addition to several series.
Any suggestions? Thanks
回答1:
You can loop over series and in the loop you can put
With currentSeries.points.first
.label = "lable name"
.font = font(...)
...
End With
This way you only specify one point in each series.
回答2:
You can do this with VBA or directly.
VBA
Assuming sc is a reference to your SeriesCollection, use
Dim isr As Integer, nsr As Integer
nsr = sc.Count
Dim sr As Series
Dim dp As Point
Dim dl As DataLabel
Dim fn As Font
For isr = 1 To nsr
Set sr = sc(isr)
Set dp = sr.Points(1)
dp.HasDataLabel = True
Set dl = dp.DataLabel
dl.Text = "<your contents>"
Set fn = dl.Characters.Font
' Format the data label
Next isr
You have to replace "<your contents>".
Note that here you can set a reference to an Excel range instead of setting a fixed value for the data label, which appears to be your intention.
Direct operation
There is something much easier than what you are doing.
To select a single data point, click on the target series, and then: 1) click again on the target data point, or 2) press the right arrow, to select the first data point.
Then to add a data label, right click on the data point, and Add Data Label.
Then to select a single data label, click on the data label once (this selects all data labels for the series, even if there is only one), and then: 1) click again on the target data label, or 2) press the right arrow, to select the first data label.
Then to edit the data label, go to the formula bar and type the contents that you want. Format at will. Note that here you can set a reference to an Excel range instead of typing text directly into the data label, which appears to be your intention.
来源:https://stackoverflow.com/questions/27806813/format-data-labels-for-each-series-in-a-chart