如何调整我的VBA以包含第二系列数据

时间:2015-12-01 11:08:19

标签: excel vba excel-vba

当我使用下面的宏时,它仅适用于一个系列数据集。我如何调整它以便我可以包含多个系列?

目前,如果我尝试使用它标记第二组,它将删除第一组,依此类推......

提前致谢

Sub AddXYLabels()
    If Left(TypeName(Selection), 5) <> "Chart" Then
        MsgBox "Please select the chart first."
        Exit Sub
    End If
    Set StartLabel = _
    Application.InputBox("Click on the cell containing the first(top) label", Type:=8)
    Application.ScreenUpdating = False
    For Each pt In ActiveChart.SeriesCollection(1).Points
        pt.ApplyDataLabels xlDataLabelsShowValue
        pt.DataLabel.Caption = StartLabel.Value
        Set StartLabel = StartLabel.Offset(1)
    Next
End Sub

1 个答案:

答案 0 :(得分:1)

由于您只对一个系列

进行了硬编码,因此您的代码不适用于该系列的其余部分
Sub AddXYLabels()
If Left(TypeName(Selection), 5) <> "Chart" Then
MsgBox "Please select the chart first."
Exit Sub
End If
 Dim mySeries As Series
    Dim seriesCol As SeriesCollection
    Dim I As Integer
    I = 1
    Set seriesCol = ActiveChart.SeriesCollection
    For Each mySeries In seriesCol
        Set mySeries = ActiveChart.SeriesCollection(I)
        With mySeries
            .ApplyDataLabels xlDataLabelsShowValue
                        
        End With
        I = I + 1
    Next
End Sub

请试试这个

&#13;
&#13;
{{1}}
&#13;
&#13;
&#13;