Labeling the Data points

  • Thread starter Thread starter Sam Beardsley
  • Start date Start date
S

Sam Beardsley

I am importing the closing prices of the Dow 30
industrials on a daily basis. Col 1 is the stocks
alphabetically. Row 1 is the date. I chart all 30 as a
line graph w date on the Y. How can I put a data label
ONLY on the first data point of the year for each of the
30 stocks?

Thanks in advance for your assistance!
Sam
 
Sam -

You can select the series, then select the first point, then press
Ctrl-1 (numeral one) or choose Selected Data Point from the Format menu,
and click on the Data Labels tab to add a label to just that point. Use
one of the options for what to label in the dialog, or just use any
label, and edit the text manually after pressing Okay. You can link the
label to a cell by selecting the single label (two single clicks, so
it's surrounded by a gray rectangle), pressing =, and clicking on the cell.

To do a whole bunch of series in a chart, you'll prefer to let a macro
do it for you. I have an example on my web site that puts a label on the
last point of each series in a chart. The modified code for the first
point is like this:

Sub FirstPointLabel()
Dim mySrs As Series
Dim nPts As Long, iPt As Long
Dim ErrNum As Long
If ActiveChart Is Nothing Then
MsgBox "Select a chart and try again.", vbExclamation, "No Chart
Selected"
Else
For Each mySrs In ActiveChart.SeriesCollection
With mySrs
nPts = .Points.count
For iPt = 1 To nPts
On Error Resume Next
mySrs.Points(iPt).HasDataLabel = True
mySrs.Points(iPt).DataLabel.Text = mySrs.name
ErrNum = Err.Number
On Error GoTo 0
If ErrNum = 0 Then Exit For
Next
End With
Next
End If
End Sub

Put this into a regular code module, select a chart, and run the macro.

The macro has a simple check that a chart is selected (otherwise there's
no active chart and it won't work, eh?), plus a loop with an error trap
that skips a point if for some reason it isn't charted. If there's a
blank in the cell, for example, the point doesn't appear in the chart,
so it can't be labeled; the loop proceeds to the next point, until it
finds one it can label. Then it's on to the next series.

- Jon
 
Back
Top