macro to apply data labels to points of an xy scatter graph

Joined
Sep 10, 2008
Messages
2
Reaction score
0
I have created a macro to add data labels to an xy scatter chart. It works fine in Excel 2007, but when my 2003 users filter a column, they receive the error message
Run-time error '1004': Unable to set the HasDataLabel property of the Points class
.

I have found an old thread on this site regarding this (click here for link) that describes how to fix this problem by inserting error avoidance code, but I dont know where I should insert it in my module. This is my existing code:

Code:
Sub AttachLabelsToPoints()
 
	 'Dimension variables.
	Dim Counter As Integer, ChartName As String, xVals As String
	Dim lngChtCounter As Long
	Dim lngSeries As Long
 
	 ' Disable screen updating while the subroutine is run.
	Application.ScreenUpdating = False
 
	For lngSeries = 1 To Chart1.SeriesCollection.Count
		 'Store the formula for the first series in "xVals".
		xVals = Chart1.SeriesCollection(lngSeries).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.
		With Chart1.SeriesCollection(lngSeries)
			lngChtCounter = 0
			For Counter = 1 To Range(xVals).Cells.Count
				If Not Range(xVals).Cells(Counter, 1).EntireRow.Hidden Then
					lngChtCounter = lngChtCounter + 1
					.Points(lngChtCounter).HasDataLabel = True
					.Points(lngChtCounter).DataLabel.Text = Range(xVals).Cells(Counter, 1).Offset(0, -1).Value
					.Points(lngChtCounter).DataLabel.Position = xlLabelPositionCenter
					.Points(lngChtCounter).DataLabel.Font.ColorIndex = 2
					.Points(lngChtCounter).DataLabel.Font.Name = "Arial"
					.Points(lngChtCounter).DataLabel.Font.Size = 11
				End If
			Next Counter
			 If lngSeries = 1 Then .DataLabels.Font.ColorIndex = 2
			 If lngSeries = 2 Then .DataLabels.Font.ColorIndex = 3
		End With
	Next
	Application.ScreenUpdating = True
 
End Sub

Hoping someone can help me with this.
Many thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top