Greg
Greg

Reputation: 43

How to delete series in chart if series name = "#N/A"

I have a dynamic chart which may sometimes include empty data series in excel vba. When there is an empty data series, only the legend entry shows as "#N/A". I am trying to build a code which will delete the data series entirely if the series name is equal to "#N/A." Below is a copy of the code I have build however I get an invalid parameter error at the "for each..." line.

Dim n As Variant
ActiveChart.PlotArea.Select
For Each n In ActiveChart.SeriesCollection(n)
    If ActiveChart.SeriesCollection(n).Name.Text = "#N/A" Then
        ActiveChart.SeriesCollection(n).Delete
    End If
Next n

Upvotes: 0

Views: 1876

Answers (1)

Tim Wilkinson
Tim Wilkinson

Reputation: 3801

Try this.

Dim n As Series
ActiveChart.PlotArea.Select 
For Each n In ActiveChart.SeriesCollection
    If n.Name = "#N/A" Then
        n.Delete
    End If
Next n

Upvotes: 1

Related Questions