Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to add label to a point

4 views
Skip to first unread message

Seth Clark

unread,
Jul 4, 1999, 3:00:00 AM7/4/99
to
Now I know if the XValues of the series contains label names, you can add
the labels easily. But I have points specified by two coordinates (so my
XValues is an array of numbers and my Values is an array of numbers) and I
want to add labels to those points. Is there anyway to do this without
adding a text box?

Thank you
Seth Clark

John Green

unread,
Jul 5, 1999, 3:00:00 AM7/5/99
to
Hi Seth,

The following code creates labels in a chart from the text in the range
B4:G4. The range has 6 entries, corresponding to the number of x values in
the chart. The code works with an XY chart as well as other types of charts.
The code is taken from "Excel 2000 VBA Programmer's Reference", written by
myself and Stephen Bullen:

Sub AddDataLabels()
Dim SalesSeries As Series
Dim pts As Points
Dim pt As Point
Dim rng As Range
Dim i As Integer

Set rng = Range("B4:G4")
Set SalesSeries = ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1)
SalesSeries.HasDataLabels = True
Set pts = SalesSeries.Points
For Each pt In pts
i = i + 1
pt.DataLabel.Text = "=" & rng.Cells(i).Address _
(RowAbsolute:=True, _
ColumnAbsolute:=True, _
ReferenceStyle:=xlR1C1, _
External:=True)
pt.DataLabel.Font.Bold = True
pt.DataLabel.Position = xlLabelPositionAbove
Next pt
End Sub

This code links the data labels to the worksheet text, and the labels
automatically update if the cell values change. If you want to assign fixed
labels to the data points, change the For...Next loop to:

For Each pt In pts
i = i + 1
pt.DataLabel.Text = rng.Cells(i).Value
pt.DataLabel.Font.Bold = True
pt.DataLabel.Position = xlLabelPositionAbove
Next pt

HTH,

John Green - Excel MVP
Sydney
Australia

In article <7lnve5$ajl$1...@solaris.cc.vt.edu>, Seth Clark wrote:
> From: "Seth Clark" <scl...@vt.edu>
> Newsgroups: microsoft.public.excel.programming
> Subject: how to add label to a point
> Date: Sun, 4 Jul 1999 11:40:38 -0400

0 new messages