• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User


  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Direct labelling excel graphs using VBA - same color for line and label


New Member
I have attached a VBA code and an Excel file with the graph I'm trying to re-style.
That piece of code was very useful to make direct labeling over a line graph in excel. A suggestion from PolicyViz website.
However I would like to go further and apply, automatically, the same color code (actually same format/style, but at least the color) from the line to the label to boost the visualization experience. Especially if your graphs as many series (lines) this will be too burdensome and inefficient to do on a series by series basis, manually.
I'm not even in control of the colors excel is choosing for the lines ....
It would be ideal to embed this styling in the macro, so all could be done in one go, and fully automatically.
Can someone help?



Excel Ninja
Staff member

Firstly, Welcome to the Chandoo.org Forums

I would tackle this issue totally differently to what you proposed

I would first add another 10 dummy ranges as shown in the picture below
These will all have the value =NA() except the last point
Then add those new 10 series to the chart
Then add a Data Label to each new series
Change the Data Label to show Series Name instead of Value
Change the Format to match the corresponding line
If you don't like the look of those 10 lines of errors shift them down or to the side out of the way

This is totally dynamic as long as you insert new data before the last point

see attached file



New Member
Hi Hui,
Thank you for your reply. I do like your approach as it doesn't require VBA, which I'm not an expert at all.
Actually I did try that approach before, with similar results as you (I did not use the NA filling, just left it blank, makes no difference). You can even choose position 2014 or 2013 if you prefer the label to be more inside the graph.

The part I don't like about this approach, is that you still have to manually do a lot of stuff (add the label, change to series name, format the labels to match the series - find out which colour is that exactly ...). If it was only one or two graphs, once in a while, fine. But I'm trying to set up a regular flow of statistics with several graphs. So I was hoping I could automatize more the process by choosing the colors upfront and having them fixed for both lines and labels (assuming the number of series would remain the same) ...


Excel Ninja
Staff member
The following code will add the Series Name to the last point on each series in each chart on the activeworksheet

Sub Add_Trailing_Series_Name()

For x = 1 To ActiveSheet.ChartObjects.Count
  For i = 1 To ActiveChart.FullSeriesCollection.Count
  Selection.ShowSeriesName = True
  Selection.ShowValue = False
  ActiveChart.FullSeriesCollection(1).HasLeaderLines = False
  np = ActiveChart.FullSeriesCollection(i).Points.Count
  For j = 1 To np - 1
  Next j
  With ActiveChart.FullSeriesCollection(i).Points(ActiveChart.FullSeriesCollection(i).Points.Count).DataLabel
  .Format.TextFrame2.TextRange.Font.Bold = msoTrue
  .Format.TextFrame2.TextRange.Font.Size = 12
  .Format.TextFrame2.TextRange.Font.Fill.Visible = msoTrue
  .Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = _
  End With
  Next i
Next x

End Sub
or see the attached file

Please let me know what you think


Han Ki Nam

New Member
>>> use code - tags <<<
Sub LabelLastPoint()
Dim mySrs As Series
Dim nPts As Long
For Each mySrs In ActiveChart.SeriesCollection
  With mySrs
    nPts = .Points.Count
    mySrs.Points(nPts).ApplyDataLabels _
       Type:=xlDataLabelsShowValue, _
       AutoText:=True, LegendKey:=False
    mySrs.Points(nPts).DataLabel.Text = mySrs.Name
    mySrs.DataLabels.Font.ColorIndex = mySrs.Border.ColorIndex
  End With
End Sub
Last edited by a moderator: