ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA to check if there is a data series in a chart? (https://www.excelbanter.com/excel-programming/303191-re-vba-check-if-there-data-series-chart.html)

Jon Peltier[_7_]

VBA to check if there is a data series in a chart?
 
SeriesCollection.Count tells you how many series are present. They are
numbered 1 to N, and if SeriesCollection(i) exists, so does
SeriesCollection(i-1).

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______

JB2004 wrote:

I am trying to create a macro that will update a chart based on a
yes/no pull down menu's. It will grab data from other worksheets if
the pull down menu indicates yes. A total of five data sets are
possible. The problem I have is the logic that would determine if
you need to add a newdata series or if you can overwrite an existing
one. The logic would have to check if a seriescollection(1) or
seriescollection(2)....etc. are there if not create one.



Jon Peltier[_7_]

VBA to check if there is a data series in a chart?
 
Here's one that's a little simpler:

With ActiveSheet.ChartObjects(1).Chart
Do While .SeriesCollection.Count N
.SeriesCollection(.SeriesCollection.Count).Delete
Loop
End With

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______

JB2004 wrote:

Thank you for your response.

Based off your response, I was able to develope a If...then and For...Next loop to check the seriescollection count to determine if a series needs to be deleted.

Below is the code I used:

Sheet1.Select
ActiveSheet.ChartObjects(1).Select
'N is the number of series selected by user
N = 2
'X is the total number of series ploted on chart
X = ActiveChart.SeriesCollection.Count
Z = X - N
If Z 0 Then
For G = 1 To Z + 1
X = ActiveChart.SeriesCollection.Count
ActiveChart.SeriesCollection(X).Delete
G = G + 1
Next
End If




All times are GMT +1. The time now is 12:15 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com