Moving Charts in a Macro

gage25

New Member
Joined
Oct 16, 2002
Messages
16
I am needing some help. I have made a macro that will build a chart, but I want to be able to move the chart to a specific location in the sheet where the graph will be. I go through the steps while recording the macro, but when I get ready to run the macro, I run into an error, everytime! What is Excel doing that is screwing this up? Is there a way to modify the macro VBA to achieve this?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
This code creates a chart and anchors it to cell D2:

Code:
    Charts.Add
    With ActiveChart
'       *** Change sheet and range references to suit ***
        .SetSourceData Source:=Sheets("Sheet1").Range("A1:B5")
'       *** Change type to suit ***
        .ChartType = xlColumnClustered
'       *** Change sheet name to suit ***
        .Location Where:=xlLocationAsObject, Name:="Sheet1"
'       *** Change top left cell location to suit ***
    End With
'   Location method creates new object so start again
    With ActiveChart
'       *** Change top left cell location to suit ***
        .Parent.Top = Range("D2").Top
        .Parent.Left = Range("D2").Left
    End With
End Sub
 

Iridium

Well-known Member
Joined
Jul 15, 2002
Messages
2,831
Another way though Andrew's is probably better as you can ock it to a cell rather than playing around with the width/left values

Application.Worksheets("Sheet").Activate
Dim rng As Range
Set rng = Application.Names("datarange").RefersToRange

Dim cht As ChartObject
Set cht = ActiveSheet.ChartObjects.Add _
(Left:=5, Width:=1000, Top:=1490, Height:=750)
With cht
.Chart.ChartType = xlXYScatter
.Chart.SetSourceData rng, xlColumns
.Chart.Location Where:=xlLocationAsObject, Name:="Sheet"
.Chart.HasTitle = True
.Chart.ChartTitle.Text = "='Sheet'!R79C1"
.Chart.Axes(xlCategory, xlPrimary).HasTitle = True
.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Actuation no."
.Chart.Axes(xlValue, xlPrimary).HasTitle = True
.Chart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Title"
End With
etc. etc.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,951
Messages
5,834,535
Members
430,295
Latest member
amdis

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
Top