Loop through chart series names
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Loop through chart series names
Hi,
Is there a way to loop through all series names in a chart and delete those series where the name begins "series", in other words, all series that have not otherwise been re-named specifically?
Is there a way to loop through all series names in a chart and delete those series where the name begins "series", in other words, all series that have not otherwise been re-named specifically?
Nathan
There's no place like home.....
There's no place like home.....
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Loop through chart series names
Apologies, I should probably be a little more specific. I have various charts each with 12 series, however in some cases some of those series are completely nul. Each series is named as part of a routine, but the "nul" series (for some reason) do not get named but retain their original names and show in the legend. I am trying to avoid nul series from both the chart and the legend (without changing the source ranges). Hope this makes sense.
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78609
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Loop through chart series names
You have to loop backwards (see Looping backwards to delete items):
Code: Select all
Sub DeleteSeries()
Dim cht As Chart
Dim i As Long
' Modify as needed
Set cht = ActiveSheet.ChartObjects("Chart 1").Chart
For i = cht.SeriesCollection.Count To 1 Step -1
If cht.SeriesCollection(i).Name Like "Series*" Then
cht.SeriesCollection(i).Delete
End If
Next i
End Sub
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Loop through chart series names
Hans, I get a 1004 "Unable to get the name property of the series class". When I look at the series tab of the source data dialogue, there are series10, series11 etc but the name field is blank. So maybe delete un-named series?
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78609
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Loop through chart series names
Does this work?
Code: Select all
Sub DeleteSeries()
Dim cht As Chart
Dim i As Long
Dim strName As String
' Modify as needed
Set cht = ActiveSheet.ChartObjects("Chart 1").Chart
For i = cht.SeriesCollection.Count To 1 Step -1
strName = ""
On Error Resume Next
strName = cht.SeriesCollection(i).Name
On Error GoTo 0
If strName = "" Or strName Like "Series*" Then
cht.SeriesCollection(i).Delete
End If
Next i
End Sub
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Loop through chart series names
no. Delete method of series class failed.
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78609
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Loop through chart series names
Could you post a sample workbook? I cannot reproduce your errors.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Loop through chart series names
Sure. I fear that this may be getting a little out of control, I am fixing one thing and breaking another.
You do not have the required permissions to view the files attached to this post.
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78609
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Loop through chart series names
Which selections/actions in the userform cause the error? I enabled the delete code in your workbook and played around with it a bit but couldn't cause an error message to appear.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Loop through chart series names
I put the delete series code at the bottom of the updateone macro, which executed ok but did not delete the unwanted series. When I commented out the 'on error resume next' from the top of that routine, then I get the error.
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78609
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Loop through chart series names
Of course you can't remove or comment out On Error Resume Next. I didn't put it there for nothing!
How do you create the "nameless" series? Whenever I manipulate a chart, the series are named co1, co2, ..., co12.
How do you create the "nameless" series? Whenever I manipulate a chart, the series are named co1, co2, ..., co12.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Loop through chart series names
Companies 10, 11 & 12 do not contain all metrics, so if you select for example metric 9, I see companies 1 to 9 (that have detail) and series 10, 11, 12 that do not. Nothing is visible in the plot area for the 3 eroneous series, but they do appear in the legend.
You do not have the required permissions to view the files attached to this post.
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78609
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Loop through chart series names
I get this:
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Loop through chart series names
Hans, were you using 2007 or 2010 by any chance? Further testing has revealed that the problem for me arises in 2003 but not 2007.
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78609
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Loop through chart series names
I have tested it both in Excel 2003 SP3 and in Excel 2007 SP2.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Loop through chart series names
It's definitely a bit odd. On 3 different machines with 2003, I get the same problem, yet 2 machines with 2007 and it works fine. Not to worry though, I found a solution by creating blank series in the database (using zeros) so that each series exists and can be named. They can then be turned off (deleted) with the checkbox.
Thanks for your time with this.
Thanks for your time with this.
Nathan
There's no place like home.....
There's no place like home.....
-
- 5StarLounger
- Posts: 818
- Joined: 24 Jan 2010, 15:56
Re: Loop through chart series names
Change the delete line from:
to
Code: Select all
.SeriesCollection(i).Delete
Code: Select all
.SeriesCollection(i).ChartType = xlColumnClustered
.SeriesCollection(i).Delete
Regards,
Rory
Rory