Microsoft Office Tutorials and References
In Depth Information
Using VBA to Display Arbitrary Data Labels on a Chart
Figure 18-9: An XY chart with no data labels.
The DataLabelsFromRange procedure works with the first chart on the active sheet. It
prompts the user for a range and then loops through the Points collection and changes the
Text property to the values found in the range.
Sub DataLabelsFromRange()
Dim DLRange As Range
Dim Cht As Chart
Dim i As Integer, Pts As Integer
‘ Specify chart
Set Cht = ActiveSheet.ChartObjects(1).Chart
‘ Prompt for a range
On Error Resume Next
Set DLRange = Application.InputBox _
(prompt:=”Range for data labels?”, Type:=8)
If DLRange Is Nothing Then Exit Sub
On Error GoTo 0
‘ Add data labels
Cht.SeriesCollection(1).ApplyDataLabels _
Type:=xlDataLabelsShowValue, _
AutoText:=True, _
LegendKey:=False
‘ Loop through the Points, and set the data labels
Pts = Cht.SeriesCollection(1).Points.Count
For i = 1 To Pts
 
Search JabSto ::




Custom Search