1

I am trying to add labels to a chart with four series. The series is Label, X, Y, size.

The VBA script below works well for one series. But I am trying to plot 4 data series (because I want to have different colors), and I get an error. What is the problem?

Sub AttachLabelsToPoints()

   'Dimension variables.
   Dim Counter As Integer, ChartName As String, xVals As String
    Dim rngCell As Range

   ' 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.
   Counter = 1
    For Each rngCell In Range(xVals).SpecialCells(xlCellTypeVisible)
        With ActiveChart.SeriesCollection(1).Points(Counter)
            .HasDataLabel = True
            .DataLabel.Text = rngCell.Offset(0, -1).Value
            Counter = Counter + 1
        End With
    Next

End Sub
4

0 回答 0