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:

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,166
Try something like this...
Code:
Set mychart = ActiveSheet.ChartObject[COLOR="Red"]s[/COLOR](1)
 

marlonsaveri

Board Regular
Joined
Jan 28, 2011
Messages
68
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"
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,166
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)
 

marlonsaveri

Board Regular
Joined
Jan 28, 2011
Messages
68
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
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,166
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
 

marlonsaveri

Board Regular
Joined
Jan 28, 2011
Messages
68
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]
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,166
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)
 

marlonsaveri

Board Regular
Joined
Jan 28, 2011
Messages
68
\o/ Thanks AlphaFrog.

But it's strange; maybe because MyGraf, a variable, is not inside ActiveSheet as ChartObjects is, a property.
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,166
What?, I'm not understanding, with all, the commas and, sentence, , fragments.

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

Forum statistics

Threads
1,081,560
Messages
5,359,608
Members
400,538
Latest member
leon_oscar

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top