Move a chart in VBA

Lucilla

New Member
Joined
Apr 13, 2011
Messages
15
Hello guys,
I have a big problem. I am writing a macro to create a graph from a table. then I want that this graph is moved to another sheet in the same workbook, but excel crashs each time and then it recovers the file without the last changes I have done :(
I do't know how to do it, because the same instruction in another macro worked...
this is the code of the function:
Code:
Function AddTrend(KPI, EqType, StartEq, EndEq)
    Range("A1").Select
    Month_Range = Range(Cells(StartEq, 5), Cells(EndEq, 5)).Value
    PT_Name = Cells(2, 6).Value
    PT_Range = Range(Cells(StartEq, 6), Cells(EndEq, 6)).Value
    TGT_Name = Cells(1, 11).Value
               KPI_Column = 7
            TGT_Column = 11
            CaptionAxis = "[%]"
            ActiveSheet.Shapes.AddChart.Select
            ActiveChart.ApplyChartTemplate ( _
                "TREND-.crtx")
        '    ActiveChart.Name = KPI
    KPI_NAME = Cells(2, KPI_Column)
    KPI_Range = Range(Cells(StartEq, KPI_Column), Cells(EndEq, KPI_Column)).Value
 
    ActiveChart.SeriesCollection(1).Name = KPI_NAME     ActiveChart.SeriesCollection(1).Values = KPI_Range
    ActiveChart.SeriesCollection(2).Name = PT_Name     ActiveChart.SeriesCollection(2).Values = PT_Range
    ActiveChart.SeriesCollection(1).XValues = Month_Range    With ActiveChart.ChartTitle
        .Caption = KPI
    End With
    With ActiveChart.Axes(xlValue, xlSecondary)
        .HasTitle = True
        .MaximumScaleIsAuto = True
        .MinimumScaleIsAuto = True
        With .AxisTitle
            .Caption = "Production Time [h]"
        End With
    End With
    With ActiveChart.Axes(xlValue, xlPrimary)
        .HasTitle = True
        .MaximumScaleIsAuto = True
        .MinimumScaleIsAuto = True
        With .AxisTitle
            .Caption = CaptionAxis
        End With
    End With
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Graph - "
 
    ActiveChart.Location Where:=StartEq
End Function

it works until
Code:
 ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="3 "
then it craschs.

someone can help me!?!? please!!!
just a note: TREND-.crtx is a template I created.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi

I just encountered the same problem with my excel.
I figured out that as soon as you leave out a name for the new chartsheet, this code works fine, so try the same code just without the name ext

ActiveChart.Location Where:=xlLocationAsNewSheet
 
Upvote 0
Simpler to use:
Code:
activeworkbook.charts.add
to add a new chart sheet in the first place. ;)
 
Upvote 0
Thanks

Will definitely use that instead in the future.

Seeing that you've just posted I'm hoping if you know the code to change the seriescollection of a chart's outline color to black

I tried this code:

activechart.seriescollection(1).format.line.forecolor.rgb = rgb(0,0,0)

It doesnt throw an error but also doesnt change the color of the series's outline? Any suggestions how to change the color
 
Upvote 0
That works for me.
 
Upvote 0
It doenst work for me, but just tried
Code:
ActiveChart.SeriesCollection(1).Border.ColorIndex = 1
and that seemed to do the trick, thanks anyway
 
Upvote 0
What kind of chart?
 
Upvote 0
No - instead of:
Code:
ActiveSheet.Shapes.AddChart.Select
 
Upvote 0
ok, thanks! I will test the solution with the entire code and I will let you know!

Thanks a lot!!!
 
Upvote 0

Forum statistics

Threads
1,216,128
Messages
6,129,035
Members
449,482
Latest member
al mugheen

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