See my annotations to your code below.
You define cht in line [A], then delete the chart it refers to in line [B].
You do not redefine cht, but reference it again in line [C]. This causes the
error that sends you to EndCode.
You really don't need line [A] at the top. Move it to just above line [C].
Also, you should step through your code to find issues like this. Put your
cursor in the procedure in the VB Editor and press F8 to execute one step.
The next line to be executed will be highlighted yellow, so you will be able
to follow program flow.
- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______
> Hi,
> Can anybody find what is wrong with this code? It will just make a
[quoted text clipped - 11 lines]
> Sub UppdateChartCF()
> Dim cht As Chart
[A] Set cht = Sheet1.ChartObjects("R_CF").Chart
> On Error Resume Next '(if no chartobject)
[B] Sheet1.ChartObjects("R_CF").Delete
> On Error GoTo EndCode
>
[quoted text clipped - 3 lines]
> .Name = "R_CF"
> End With
[C] With cht
> .SetSourceData Sheet2.Range("CHT_CF_RNG"), PlotBy = xlRows
> .HasTitle = True
[quoted text clipped - 45 lines]
> On Error GoTo 0
> End Sub
tskogstrom - 24 Oct 2006 09:18 GMT
Thanks, I'll look into this.
I have some more charts- it might be a lot of code to arrange this.
Maybe I will do a routine to copy-paste a unvisible template instead,
if the user want it by button or the chart is deleted.
What would you do?
/Regards
tskogstrom
Jon Peltier skrev:
> See my annotations to your code below.
>
[quoted text clipped - 93 lines]
> > On Error GoTo 0
> > End Sub
Jon Peltier - 24 Oct 2006 17:22 GMT
If the number of series in the chart are the same, I might just change the
source data of each.
Here's an example for one series:
With cht.SeriesCollection(1)
.Name = Sheet2.Range("CHT_R_INVEST")
.Values = Sheet2.Range("CHT_" & _
Sheet1.Range("SCENARIO_NO").Value & "CF" & _
Sheet1.Range("RAPP_TILLF").Value & "_INVESTAR")
End With
In any case, though the formatting code can be streamlined, it's not really
excessive. Templates are good too, if you can ensure that the user doesn't
mess around with them. User defined chart types are also a good choice, if
they work on that machine (mine are broken).
- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______
> Thanks, I'll look into this.
> I have some more charts- it might be a lot of code to arrange this.
[quoted text clipped - 111 lines]
>> > On Error GoTo 0
>> > End Sub