Trap error for trendline clear button?

P

Phil C

Hi everyone

I have 2 data series on an XY scattergraph. I have created buttons for
turning the associated trendlines ON/OFF.
Trendline 1 is the trendline for series 1.
The sheet opens with trendline 1 on and trendline 2 off
One button (A) turns trendline 1 off and trendline 2 on
The other button (B) does the opposite (turns trendline 2 off and trendline
1 on)
The idea is that one trendline replaces the other.
However, if the user clicks the 'wrong button' (e.g clicks A when trendline
1 is already off) I get a runtime error 1004 (unable to get the trendline
properties of the series class). Fair enough.
What is the simplest way to trap this error?

Existing code attached to one of the buttons ('1 off, 2 on')

ActiveSheet.ChartObjects("Chart 7").Activate
ActiveChart.SeriesCollection(1).Trendlines(1).Select
Selection.Delete
ActiveChart.SeriesCollection(2).Select
ActiveChart.SeriesCollection(2).Trendlines.Add(Type:=xlLinear,
Forward:=0, _
Backward:=0, DisplayEquation:=False, DisplayRSquared:=False).Select

Thanks for your help.

Phil
 
A

Andy Pope

Hi Phil,

Could you make use of the Trendlines.Count property?

If ActiveChart.SeriesCollection(1).Trendlines.Count = 1 Then
ActiveChart.SeriesCollection(1).Trendlines(1).Delete

Cheers
Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top