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
 

Some videos you may like

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

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,781
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
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,105,936
Messages
5,508,223
Members
408,670
Latest member
lhmwnrexcel

This Week's Hot Topics

Top