How could I select a chart within a sheet?

marlonsaveri

Board Regular
Joined
Jan 28, 2011
Messages
68
I've created some examples trying to discover:

Code:
  Dim mychart As ChartObject
    Set mychart = ActiveWorkbook.ActiveSheet.ChartObject(1)
    Debug.Print mychart.Name
Code:
Sub EditSeriesName()
    Charts(1).Select
    ActiveChart.SeriesCollection(4).Name = "Nova"
End Sub
but I can't select a chart within a sheet, ie, a ChartObject. How could I do?

Thanks
 
Last edited:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try something like this...
Code:
Set mychart = ActiveSheet.ChartObject[COLOR="Red"]s[/COLOR](1)
 
Upvote 0
Studying "Debug.Print ActiveChart.Index", I see that the "index" applies only to charts that take up entire worksheets - charts - not ChartObject or I'm putting a invalid index in my case.

Thanks.

I've tried a macro, it tells me to use "ActiveSheet.ChartObjects("name of the chart").Activate"
 
Upvote 0
I'm able to use the index reference on charts within a worksheet.

Did you note the S at the end of ChartObjects. You had omitted it in your code in the original posting #1.
Code:
Set mychart = ActiveSheet.ChartObject[B]s[/B](1)
 
Upvote 0
I noted, maybe it's becaus I don't know the right index. But, why is it not working either?

Code:
Sub TakeIndex()
    Debug.Print ActiveChart.Index
End Sub
 
Upvote 0
The ChartObject is the Parent of the ActiveChart. The code below will return the index of the parent ChartObject of the ActiveChart.

Code:
Sub TakeIndex()
    Debug.Print ActiveChart.Parent.Index
End Sub
 
Upvote 0
Thanks, now it's working!

To finish my lesson, I tried:

Code:
[COLOR=Blue]Sub[/COLOR] Experience()
[COLOR=Blue]    Dim[/COLOR] MyGraf As ChartObject
    [COLOR=Blue]Dim[/COLOR] lastcell As Long

[COLOR=Blue]    Debug.Print [/COLOR]ActiveChart.Parent.Index
    Range("A2").Select
    ActiveSheet.ChartObjects(1).Select
[COLOR=Blue]    Set[/COLOR] MyGraf = ActiveSheet.ChartObjects(1)
    MsgBox "Ok So Far" [COLOR=PaleGreen]'OK[/COLOR]

    [COLOR=Red]lastcell = ActiveSheet.MyGraf.Chart.SeriesCollection.Count[/COLOR] [COLOR=Lime]'problem here[/COLOR]
    MyGraf.Chart.SeriesCollection(lastcell).Name = "LastCell"
[COLOR=Blue]End Sub[/COLOR]
 
Upvote 0
Code:
lastcell = MyGraf.Chart.SeriesCollection.Count  [COLOR="Green"]' Removed ActiveSheet.[/COLOR]

I don't understand why this works now but not before.
Set MyGraf = ActiveSheet.ChartObjects(1)
 
Upvote 0
\o/ Thanks AlphaFrog.

But it's strange; maybe because MyGraf, a variable, is not inside ActiveSheet as ChartObjects is, a property.
 
Upvote 0
What?, I'm not understanding, with all, the commas and, sentence, , fragments.

But it really doesn't matter. Glad it worked out for you.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top