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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

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