Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default add labels to line chart

Hi All

I have a line chart with 11 series. I want to add a data label to the
last point only for each of the series, showing the value and series
name with the legend key. I would like to have code that will cycle
through each of these series and add that information (note, this is
probably simple, but I am a beginner with VBA)
Here is some code that I recorded, that works for one of the series
(Series 6):

Sub Macro3()
x = Sheets("Data").Cells(1, 1).Value
Sheets("Chart").Select
ActiveChart.SeriesCollection(6).Points(x).ApplyDat aLabels
ActiveChart.SeriesCollection(6).Points(x).DataLabe l.Select
Selection.ShowSeriesName = -1
Selection.ShowLegendKey = -1
ActiveChart.ChartArea.Select
End Sub

Note: I made the "point" a variable because the chart data is
dynamic. This code refers to a cell (Sheet "Data" cell A1) that
counts the number of rows of data (will expand and contract depending
on dates I choose). That way, I am always getting just the last
point. This seems to work fine, I just need code to cycle through all
of the series and add the same label.

Many thanks for your help
Steve
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default add labels to line chart



Sorry, forgot to say that I am using Excel 2007, if that makes a
difference.
Thanks
steve
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default add labels to line chart

If you look, you'll see

SeriesCollection(6)

in your code. This means the code is only applying a label to the 6th
series. Introduce a variable that loops through all series numbers, as I've
demonstrated in the code below. Select the chart and run this code:

Sub Macro4()
Dim iSrs As Long
Dim iPts As Long
For iSrs = 1 To ActiveChart.SeriesCollection.Count
With ActiveChart.SeriesCollection(iSrs)
iPts = .Points.Count
With .Points(iPts)
.ApplyDataLabels
.DataLabel.ShowSeriesName = -1
.DataLabel.ShowLegendKey = -1
End With
End With
Next
End Sub

Note: I changed your variable 'x' to iPts for clarity. I generalized it to
work on the active chart, and to figure out the number of points without
reading it from outside the chart. I also used some With/End With loops.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Steve Mackay" wrote in message
...
Hi All

I have a line chart with 11 series. I want to add a data label to the
last point only for each of the series, showing the value and series
name with the legend key. I would like to have code that will cycle
through each of these series and add that information (note, this is
probably simple, but I am a beginner with VBA)
Here is some code that I recorded, that works for one of the series
(Series 6):

Sub Macro3()
x = Sheets("Data").Cells(1, 1).Value
Sheets("Chart").Select
ActiveChart.SeriesCollection(6).Points(x).ApplyDat aLabels
ActiveChart.SeriesCollection(6).Points(x).DataLabe l.Select
Selection.ShowSeriesName = -1
Selection.ShowLegendKey = -1
ActiveChart.ChartArea.Select
End Sub

Note: I made the "point" a variable because the chart data is
dynamic. This code refers to a cell (Sheet "Data" cell A1) that
counts the number of rows of data (will expand and contract depending
on dates I choose). That way, I am always getting just the last
point. This seems to work fine, I just need code to cycle through all
of the series and add the same label.

Many thanks for your help
Steve



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default add labels to line chart

Sorry to keep posting replies to my own request. I found something
close to what I want, but it only gives me the name of my series. I
would also like to have the value of the point too. Any ideas?

Sub LastPointLabel()
Dim mySrs As Series
Dim nPts As Long
Sheets("Chart").Select
For Each mySrs In ActiveChart.SeriesCollection
With mySrs
nPts = .Points.Count
mySrs.Points(nPts).ApplyDataLabels _
Type:=xlDataLabelsShowValue, _
AutoText:=True, LegendKey:=True
mySrs.Points(nPts).DataLabel.Text = mySrs.Name

End With
Next
End Sub

Thanks
Steve
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default add labels to line chart

On Apr 9, 5:26 pm, "Jon Peltier"
wrote:
If you look, you'll see

SeriesCollection(6)

in your code. This means the code is only applying a label to the 6th
series. Introduce a variable that loops through all series numbers, as I've
demonstrated in the code below. Select the chart and run this code:

Sub Macro4()
Dim iSrs As Long
Dim iPts As Long
For iSrs = 1 To ActiveChart.SeriesCollection.Count
With ActiveChart.SeriesCollection(iSrs)
iPts = .Points.Count
With .Points(iPts)
.ApplyDataLabels
.DataLabel.ShowSeriesName = -1
.DataLabel.ShowLegendKey = -1
End With
End With
Next
End Sub

Note: I changed your variable 'x' to iPts for clarity. I generalized it to
work on the active chart, and to figure out the number of points without
reading it from outside the chart. I also used some With/End With loops.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. -http://PeltierTech.com
_______

"Steve Mackay" wrote in message

...

Hi All


I have a line chart with 11 series. I want to add a data label to the
last point only for each of the series, showing the value and series
name with the legend key. I would like to have code that will cycle
through each of these series and add that information (note, this is
probably simple, but I am a beginner with VBA)
Here is some code that I recorded, that works for one of the series
(Series 6):


Sub Macro3()
x = Sheets("Data").Cells(1, 1).Value
Sheets("Chart").Select
ActiveChart.SeriesCollection(6).Points(x).ApplyDat aLabels
ActiveChart.SeriesCollection(6).Points(x).DataLabe l.Select
Selection.ShowSeriesName = -1
Selection.ShowLegendKey = -1
ActiveChart.ChartArea.Select
End Sub


Note: I made the "point" a variable because the chart data is
dynamic. This code refers to a cell (Sheet "Data" cell A1) that
counts the number of rows of data (will expand and contract depending
on dates I choose). That way, I am always getting just the last
point. This seems to work fine, I just need code to cycle through all
of the series and add the same label.


Many thanks for your help
Steve


Thanks, John. Really appreciate the help.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default add labels to line chart


John,

This works well, thanks. Just one more question. If I already have
labels, then add more data and repeat the macro, I'll have two labels
for each series. Is there a way to first cycle through and delete all
existing labels?

Thanks
Steve
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default add labels to line chart

Insert one line like so:

With ActiveChart.SeriesCollection(iSrs)
.HasDataLabels = False
iPts = .Points.Count

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Steve Mackay" wrote in message
...

John,

This works well, thanks. Just one more question. If I already have
labels, then add more data and repeat the macro, I'll have two labels
for each series. Is there a way to first cycle through and delete all
existing labels?

Thanks
Steve



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default add labels to line chart

Replace this

mySrs.Points(nPts).DataLabel.Text = mySrs.Name

with this:

mySrs.Points(nPts).DataLabel.ShowSeriesName = True

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______



"Steve Mackay" wrote in message
...
Sorry to keep posting replies to my own request. I found something
close to what I want, but it only gives me the name of my series. I
would also like to have the value of the point too. Any ideas?

Sub LastPointLabel()
Dim mySrs As Series
Dim nPts As Long
Sheets("Chart").Select
For Each mySrs In ActiveChart.SeriesCollection
With mySrs
nPts = .Points.Count
mySrs.Points(nPts).ApplyDataLabels _
Type:=xlDataLabelsShowValue, _
AutoText:=True, LegendKey:=True
mySrs.Points(nPts).DataLabel.Text = mySrs.Name

End With
Next
End Sub

Thanks
Steve



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to change position of chart labels on line chart Shane Henderson[_2_] Charts and Charting in Excel 1 May 27th 11 09:31 AM
labels in line chart GrandMufti Charts and Charting in Excel 1 April 28th 08 10:55 PM
Min, Max Value Labels in Line Chart Freddy Charts and Charting in Excel 1 May 10th 06 04:07 AM
Sub-Categories for X-axis labels, line on bar chart? cfagan Charts and Charting in Excel 1 March 3rd 06 05:01 PM
Can I link timeline labels to a line chart? hizzle Charts and Charting in Excel 1 December 6th 05 09:36 AM


All times are GMT +1. The time now is 12:32 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"