Code for positioning a graph (Chart)

Bob Yerby

New Member
Joined
Jul 26, 2010
Messages
10
I am trying to create a graph which I can place in the top left hand corner of a sheet rather than in the middle of the sheet which appears to be the default. Can I do this? I have tried recording a macro to get the code, problem is that when i then run the code the graph which was called CHART 1 is now called CHART 2.
I did try ActiveChart.Name="MyGraph" but that didn't work (it was only a guess).
Any help greatly appreciated. (I do not care what the graph is called, I just want to control where it appears on the screen).

Bob
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Sub MovCht()
With ActiveSheet.ChartObjects(ActiveSheet.ChartObjects.Count)
    .Top = Range("A1").Top
    .Left = Range("A1").Left
End With
End Sub
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
Hi Bob
Welcome to the board

I usually position the chartobject when I create it, and name it immediately after.

Ex. Suppose a simple column chart, X values in Sheet1!A2:A5, Y values in Sheet1!B2:B5.

If you want to create the chart in the top left had corner of Sheet1, for ex.:

Code:
Sub CreateChart()
Dim ws As Worksheet
Dim rX As Range, rY As Range
Dim chtO As ChartObject
 
Set ws = Worksheets("Sheet1")
Set rX = ws.Range("A2:A5")
Set rY = ws.Range("B2:B5")
 
Set chtO = ws.ChartObjects.Add(0, 0, 400, 200)
chtO.Name = "Mychart"
 
With chtO.Chart
    With .SeriesCollection.NewSeries
       .ChartType = xlColumnClustered
       .Values = rY
       .XValues = rX
    End With
End With
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,133,639
Messages
5,660,016
Members
418,542
Latest member
Sandy3503

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