VBA/Macro to Create Chart and Specify its Position On a New Tab

Simonc64

Active Member
Joined
Feb 15, 2007
Messages
251
Office Version
  1. 365
Hi All

Apologies VBA isn't my thing but thinking there must be a way to do this.

My workbook has 2 worksheets, sheet1 and sheet2

My data is stored in Sheet2, cells A38:B43

I need to create a column chart in Sheet1 say in position H5:L9

The idea being that I then attach the macro to a button in Sheet1 to produce the chart

All help appreciated!

Simon
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
You can record when you build the graph and then assign this macro to your button.
 
Upvote 0
No that doesn't work which is why I asked the question - its needs to be positioned precisely in the correct place
 
Upvote 0
After recorded you can add

Code:
[COLOR=#333333]ActiveSheet.Shapes("[/COLOR][COLOR=#ff0000]Your Chart[/COLOR][COLOR=#333333]").Left = Sheet1.Cells(5, 8).Left[/COLOR]
[COLOR=#333333]ActiveSheet.Shapes("[/COLOR][COLOR=#ff0000]Your Chart[/COLOR][COLOR=#333333]").Top = Sheet1.Cells(5, 8).Top[/COLOR]
[COLOR=#333333]ActiveSheet.Shapes("[/COLOR][COLOR=#ff0000]Your Chart[/COLOR][COLOR=#333333]").Height = Sheet1.Range("H5:H9").Height[/COLOR]
[COLOR=#333333]ActiveSheet.Shapes("[/COLOR][COLOR=#ff0000]Your Chart[/COLOR][COLOR=#333333]").Width = Sheet1.Range("H9:L9").Width
[/COLOR]
 
Last edited:
Upvote 0
OK so ive managed this and it comes u with a run time error - object required - dash is my active sheet

Sub MacroChart2()
'
' Macro1 Macro
'

'
ActiveSheet.Shapes.AddChart (xlColumnClustered)
ActiveSheet.Shapes("Chart2").Left = Dash.Range("E5").Left
ActiveSheet.Shapes("Chart2").Top = Dash.Range("E5").Top
ActiveSheet.Shapes("Chart2").Width = Dash.Range("I5").Width
ActiveSheet.Shapes("Chart2").Height = Dash.Range("E5:E13").Height
ActiveChart.SetSourceData Source:=Range("Dash!$A$8:$B$13")
End Sub
 
Upvote 0
Code:
[COLOR=#333333]ActiveSheet.Shapes("Chart2").Left = [/COLOR][COLOR=#ff0000]sheets("Dash")[/COLOR][COLOR=#333333].Range("E5").Left[/COLOR]
[COLOR=#333333]ActiveSheet.Shapes("Chart2").Top =[/COLOR][COLOR=#ff0000]sheets("Dash")[/COLOR][COLOR=#333333].Range("E5").Top[/COLOR]
[COLOR=#333333]ActiveSheet.Shapes("Chart2").Width = [/COLOR][COLOR=#ff0000]sheets("Dash")[/COLOR][COLOR=#333333].Range("[/COLOR][COLOR=#ff0000]E5:I5[/COLOR][COLOR=#333333]").Width[/COLOR]
[COLOR=#333333]ActiveSheet.Shapes("Chart2").Height = [/COLOR][COLOR=#ff0000]sheets("Dash")[/COLOR][COLOR=#333333].Range("E5:E13").Height[/COLOR]
[COLOR=#333333]ActiveChart.SetSourceData Source:=Range("Dash!$A$8:$B$13")[/COLOR]

If you still have an error, it means your graph is not called Chart2, so might be easier not to name it. Maybe this below could help

Code:
Sub Graph()
'Get a column chart in Range E5:E13 From data in A8:B13 -> graph and data on sheet called Dash
Dim rng as Range

Sheets("Dash").Activate
Range("E5").Select

'Your data range for the chart
  Set rng = ActiveSheet.Range("A8:B13")


'Create a chart
  ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select

'Position it
   With Selection
    .Left = ActiveCell.Left
    .Width = Sheets("Dash").Range("E5:I5").Width
    .Top = ActiveCell.Top
    .Height = Sheets("Dash").Range("E5:E13").Height
   End With

'Give chart some data
    ActiveChart.SetSourceData Source:=rng

'Determine the chart type
  ActiveChart.ChartType = xlColumnClustered

End Sub
 
Last edited:
Upvote 0
Hi I appreciate you trying to help me, I have copied the code but still get an error "subscript out of range" and the Sheets("Dash").Activate line is highlighted as wrong?

Simon
 
Upvote 0
That means that you don't have a sheet called "Dash"
 
Upvote 0
The name of the sheet must be Dash (the one you see when not in VBA -> In VBA you would see Sheet1(Dash) in left column). If you changed the name in properties VBA (which is a very good habit by the way), so that you see something like Dash(watever name the user see) in the column, then Sheets("Dash").Activate should be replaced by
Code:
Dash.activate
 
Last edited:
Upvote 0
That's the problem, I DO have a sheet called dash as that is where all my data is held
 
Upvote 0

Forum statistics

Threads
1,213,520
Messages
6,114,101
Members
448,548
Latest member
harryls

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