删除除 i 时间序列中最后两个之外的所有数据标签

Deleting all datalabels except the last two in i time series

我需要一个宏来删除折线图中除最后两个之外的所有数据标签。 我已经设法删除了一列标签,请提供有关如何删除其余标签的任何帮助。

这是我目前使用的代码:

Option Explicit
Sub Format_linechart()

    Dim sld As Slide
    Dim shp As Shape
    Dim chart As chart
    Dim sr As Series
    Dim i As Long
    Dim Cnt As Integer
    
    Set sld = Application.ActiveWindow.View.Slide
    
    For Each shp In sld.Shapes
        If shp.HasChart Then
            Set chart = shp.chart
            For i = 1 To chart.SeriesCollection.Count
                Set sr = chart.SeriesCollection(i)
                sr.HasDataLabels = True
                sr.Points(sr.DataLabels.Count - 2).DataLabel.Delete
            Next i
        End If
    Next shp

End Sub

我也尝试过为每组数据标签添加行,但如果图表中的数据点太少,我就会收到错误消息。

Option Explicit
Sub Format_linechart()

    Dim sld As Slide
    Dim shp As Shape
    Dim chart As chart
    Dim sr As Series
    Dim i As Long
    Dim Cnt As Integer
    
    Set sld = Application.ActiveWindow.View.Slide
    
    For Each shp In sld.Shapes
        If shp.HasChart Then
            Set chart = shp.chart
            For i = 1 To chart.SeriesCollection.Count
                Set sr = chart.SeriesCollection(i)
                sr.HasDataLabels = True
                sr.Points(sr.DataLabels.Count - 2).DataLabel.Delete
                sr.Points(sr.DataLabels.Count - 3).DataLabel.Delete
                sr.Points(sr.DataLabels.Count - 4).DataLabel.Delete
                sr.Points(sr.DataLabels.Count - 5).DataLabel.Delete
                sr.Points(sr.DataLabels.Count - 6).DataLabel.Delete
            Next i
        End If
    Next shp

End Sub

对于每个系列,您可以遍历每个点并删除所需点的数据标签。 . .

Option Explicit

Sub Format_linechart()

    Dim sld As Slide
    Dim shp As Shape
    Dim chart As chart
    Dim sr As Series
    Dim i As Long
    Dim j As Long
    Dim Cnt As Integer

    Set sld = Application.ActiveWindow.View.Slide
    
    For Each shp In sld.Shapes
        If shp.HasChart Then
            Set chart = shp.chart
            For i = 1 To chart.SeriesCollection.Count
                Set sr = chart.SeriesCollection(i)
                sr.HasDataLabels = True
                For j = 1 To sr.Points.Count - 2
                    sr.Points(j).DataLabel.Delete
                Next j
            Next i
        End If
    Next shp

End Sub