title: How delete a series from an Excel chart using VBA
body: <p>I'm trying to delete the empty series from a chart in Excel 2003 using VBA. I've seen that others have had this issue in the past and I have tried all methods mentioned in their posts but have been unable to find anything that works consistently.</p>
  
  <p>The chart has 14 series in it and anywhere between 3 or 9 of them can be empty. The empty ones are always between Series 4 - 12.</p>
  
  <p>I've tried a few variations of code but this is primarily it:</p>
  
  <pre><code>Sheets("chart-1").Select
  ActiveChart.PlotArea.Select
  For i = 12 To 4 Step -1
      Dim theSeries As Series
      MsgBox (ActiveChart.SeriesCollection(i).Name)
      Set theSeries = ActiveChart.SeriesCollection(i)
      MsgBox (theSeries.Name)
      theSeries.Delete
  Next
  </code></pre>
  
  <p>I can run it successfully once for a chart, but all subsequent cycles fail with a <code>Unable to get the Name property of the Series class</code> error. It fails on the call to .Name.</p>
  
  <p>I been able to get it work by inserting integers directly, but it will only run once for all integers except 1. It run it multiple times for <code>Series(1)</code>. </p>
  
  <p>For instance if I simply call: ActiveChart.SeriesCollection(1).Delete, then the series is deleted, but if I then run it with another integer (4, 9, 12) it won't run. It will work again for 1, but only 1. It will also work once with other integers (say 4), but all subsequent calls will fail even if I change the integer to 1 or keep it as 4, or change it to some other number.</p>
  
  <p>The behaviour is really quite strange. </p>
  
  <p>Any ideas would be greatly appreciated. I can't simply call <code>ActiveChart.SeriesCollection(1).Delete</code> repeatedly because the first 3 series are always non-empty.</p>
  
  <p>Thanks.</p>
  
  <p>** Update **</p>
  
  <p>I just ran a test manually executing the following: </p>
  
  <pre><code>Sheets("ch-v2-12mth").Select
  ActiveChart.PlotArea.Select
  MsgBox (ActiveChart.SeriesCollection(1).Name)
  </code></pre>
  
  <p>I cycled through the SeriesCollection trying the numbers 1 - 16 (there are only 14 Series in the chart) to see the result.
  1 - 3 worked fine
  4 - 13 errored with <code>Unable to get the Name property of the Series class</code>
  14 worked fine
  15 - 16 errored with <code>Method 'SeriesCollection' of object '_Chart' failed</code> &lt;- not surprising given the number of series in the chart.</p>
  
  <p>This type of behaviour makes me think that there is a bug with Excel. Any other ideas?</p>