Lowpar
Lowpar

Reputation: 907

Excel Graph, if labels exist, delete them

How do I check if a chart has labels? At the moment the graph is erroring because I am trying to delete labels that do not exist.

Sub deltelabels()
Dim x As Integer

    ActiveSheet.ChartObjects("Chart 4").Activate

For x = 1 To 4
    ActiveChart.FullSeriesCollection(x).DataLabels.Delete
Next x
End Sub

Upvotes: 0

Views: 1424

Answers (1)

Lowpar
Lowpar

Reputation: 907

Modified from https://www.experts-exchange.com/questions/27542370/Check-for-existence-of-data-labels-first-chart-of-the-active-worksheet.html

Sub CheckChartLabels()
    Dim cht As Chart
    Dim ser As Series
    Dim HasLabels As Long

    Set cht = Sheets("Page 3").ChartObjects("Chart 4").Chart
    For Each ser In cht.SeriesCollection
        If ser.HasDataLabels Then ser.DataLabels.Delete
    Next

End Sub

Upvotes: 1

Related Questions