Hi. I have a macro that currently will automatically apply labels to a chart. The chart must already be made with data in this format:

label1, x1, y1
label2, x2,y2
label3,x3,y3
label4,x4,y4

and so forth

The chart is linked to the second and third columns. The macro applies the data in the first column as labels. The macro works well. However, when it runs into an # N/A value, the macro breaks. So for example, the data might look like this:

label1, x1, y1
label2, x2,y2
#N/A,#N/A,#N/A
label4,x4,y4

The macro will apply the first two labels, but then will crash when it hits the #N/A. The reason I am using the #N/A is because Excel will ignore these values for charting.

I am looking to adjust my macro to account for these values. Can someone please help me? I have thought about workarounds, such as by trying to structure the data such that there are no #N/As, but I think this is the easiest solution, unless someone has a better way to autolabel charts altogether.

COuld someone poissibly help me? Here is the code for the autolabeler:

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