Excel - VBA Activate Chart Created in Different Function - Stack Overflow
Excel - VBA Activate Chart Created in Different Function - Stack Overflow
What's the deal with Deno? We talk with a major contributor to find out. Listen now.
0 Sub CreateChart()
Dim sh As Worksheet
Dim chrt as Chart
Set sh = ActiveWorkbook.Worksheets("Sheet1")
Set chrt = sh.Shapes.AddChart.Chart
End Sub
How can I activate this chart in a different function where I want to move it to a certain cell? I am
using the following code but it keeps giving me errors and won't activate the chart. I even gave
the chart a Title and tried to use it's title t activate it but it won't recognize the name:
Sub MoveChart()
ActiveSheet.ChartObjects("chrt").Activate
With ActiveChart.Parent
.Left = Range("N2").Left
End With
vba excel
Why activate it? You may use directly the chrt object that you stored, which you may pass to a
parametrized MoveChart subroutine:
0
Sub MoveChart(ByVal chrt As Excel.Chart)
With chrt.Parent
.Left = Range("N2").Left
End With
End Sub
https://stackoverflow.com/questions/24825676/vba-activate-chart-created-in-different-function 1/3
10/2/2020 excel - VBA activate chart created in different function - Stack Overflow
with the difference that now you may move any chart you'd like.
Later edit
The second macro fails because the chart remains unnamed. Trying to access the chart by the
name of the VBA variable doesn't do the trick. So, try this:
Sub CreateChart()
With ActiveWorkbook.Worksheets("Sheet1").Shapes
Set chrt = .AddChart()
Let chrt.Name = "New chart"
End With
End Sub
Sub MoveChart()
With ActiveWorkbook.Worksheets("Sheet1")
.ChartObjects("New chart").Left = .Range("N2").Left
End With
End Sub
Don't forget to modify the code to use other chart names, other sheet names etc.
When I try this, I am unable to run the macro. It won't show up as a macro on the macro list. – art123456
Jul 18 '14 at 13:25
Oh, I guess you're not trying to program complicated stuff... usually activating sheets and objects is not very
efficient. But, okay, the mistake in the code you wrote is that the chart name is not set correctly. I'll come
back with code written your way. – user2271770 Jul 18 '14 at 13:42
Thanks so much! I am fairly new to VBA, I've only been coding in it for about 3 weeks now so I have a lot to
learn. But yes, no complicated stuff as of now haha. – art123456 Jul 18 '14 at 13:53
I get an error stating "Out of memory" at the line Let chrt.name = "New Chart" – art123456 Jul 18 '14
at 15:58
@art123456 Is chrt declared as Shape or as Chart in your CreateChart ? Please compare with the
code that I posted. – user2271770 Jul 18 '14 at 16:01
So, actually, you didn't use my code on CreateChart . :-) Okay, first of all you just said that your
declaration is Dim chrt As Chart so chrt has the type Chart not Shape . Please add in your sub,
after setting chrt , the following statement: Let chrt.Parent.Name = "New chart" , because charts don't
have names, but their parents (which are shapes) do. – user2271770 Jul 18 '14 at 17:13
@art123456 Please see the comment above, I forgot to tag your ID. – user2271770 Jul 18 '14 at 17:22
https://stackoverflow.com/questions/24825676/vba-activate-chart-created-in-different-function 2/3
10/2/2020 excel - VBA activate chart created in different function - Stack Overflow
That works wonders! Thank you so much for your help :) I do want to ask you something though if you'd be
so kind to answer for me. I have seen this before and have tried to read about it, but it's hard to understand:
ByVal – art123456 Jul 18 '14 at 18:40
I'm not sure why, but for some reason your code worked and now it doesn't anymore. I tried it that one time
and I had to go and haven't worked on the program since my last comment. Anyways, now I am getting an
error that states: The item with the specified name wasn't found on the line: .ChartObjects("New
Chart").Left = .Range("N2").Left – art123456 Jul 22 '14 at 18:54
@art123456 Well, the error tells you what's wrong. Long translation would be: in the active workbook, in the
sheet named "Sheet1" there is no chart who's name is "New Chart" . Possible errors: 1) The chart was
not created, or is created with a similar name (typos, extra spaces, case differences), 2) The chart was
created in other worksheet/workbook actually, 3) the active workbook is not the one that you want (as I said
before activating stuff to accessing stuff is weird, at least for me). So, here's your detective story: try to find
out what actually went wrong. :-) – user2271770 Jul 23 '14 at 8:28
The thing is, I know that the chart is created onto the correct sheet. Because I set eh sheet as Set sh =
ActiveWorkbook.Worksheets("TraceTable") and then I set the chart as Set chrt =
sh.Shapes.AddChart.Chart so now I know it's created onto that worksheet. The name of the chart using
Let chrt.Parent.Name = "EIT" makes the chart name "EIT", and then I call the chart later in the
MoveChart Sub using the name "EIT". This is bugging me because it definitely worked once and then I
don't know what I did to change it! :/ – art123456 Jul 23 '14 at 13:05
@art123456 Are you used to work with the Immediate Window in the VBA Editor? Because that helps you
get immediate answers about this. – user2271770 Jul 23 '14 at 13:12
@ CST-Link The immediate window? I'm not sure what that is so probably not. – art123456 Jul 23 '14 at
13:21
https://stackoverflow.com/questions/24825676/vba-activate-chart-created-in-different-function 3/3