用于向excel气泡图添加标签的VBA脚本

时间:2013-05-27 03:50:13

标签: excel vba charts

我正在尝试使用四个系列向图表添加标签。该系列是Label,X,Y,尺寸。

下面的VBA脚本适用于一个系列。但我试图绘制4个数据系列(因为我想要有不同的颜色),我得到一个错误。有什么问题?

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

0 个答案:

没有答案