chart label macro

  • Thread starter Thread starter Gklass
  • Start date Start date
G

Gklass

Here's macro for labelling XY charts.

but it only works for the the first series (you can reorder the
series)
and the labels have to be in the column (the data have to be in
colums), to the left of the X-axis.

but it works in 007

http://support.microsoft.com/kb/213750

Sub AttachLabelsToPoints()

'Dimension variables.
Dim Counter As Integer, ChartName As String, xVals As String

' Disable screen updating while the subroutine is run.
Application.ScreenUpdating = False

'Store the formula for the first series in "xVals".
xVals = ActiveChart.SeriesCollection(1).Formula

'Extract the range for the data from xVals.
xVals = Mid(xVals, InStr(InStr(xVals, ","), xVals, _
Mid(Left(xVals, InStr(xVals, "!") - 1), 9)))
xVals = Left(xVals, InStr(InStr(xVals, "!"), xVals, ",") - 1)
Do While Left(xVals, 1) = ","
xVals = Mid(xVals, 2)
Loop

'Attach a label to each data point in the chart.
For Counter = 1 To Range(xVals).Cells.Count
ActiveChart.SeriesCollection(1).Points(Counter).HasDataLabel = _
True
ActiveChart.SeriesCollection(1).Points(Counter).DataLabel.Text =
_
Range(xVals).Cells(Counter, 1).Offset(0, -1).Value
Next Counter

End Sub
 
To extend this to any series, use the appropriate index wherever this
occurs:

SeriesCollection(1)

or change the reference point so that you select a series and run a macro,
and change

ActiveChart.SeriesCollection(1)

to

Selection

Put this up front to bail out if no series is selected:

If TypeName(Selection) <> "Series" Then Exit Sub

- Jon
 
Back
Top