Resizing a chart using VBA

tmanger

Board Regular
Joined
Jul 15, 2002
Messages
101
Hello all,

I would like to start by thanking everyone. I am a "lurker" and don't post often, but has provided some fantastic solutions for my VBA and Excel sheets!!! (As well as the realization that there is much I don't know about programming). Thank you all.

My current problem is changing the size of pie chart. I cannot seem to get the code to recognize the Shape Name, despite naming the chart. I am sure it is something simple that I am overlooking and appreciate any insight. This is the code I am trying to use:

Sub Resizechart()

Range("A1:B3").Select
Charts.Add
ActiveChart.Name = "Breakfast Chart"
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:B3"), PlotBy:= _
xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
ActiveChart.HasLegend = True
ActiveChart.Legend.Select
Selection.Position = xlRight

ActiveSheet.Shapes("Chart 43").ScaleWidth 0.67, msoFalse, msoScaleFromTopLeft
ActiveSheet.Shapes("Chart 43").ScaleHeight 1.11, msoFalse, msoScaleFromTopLeft
ActiveWindow.Visible = False
Windows("MyOwnChartMaker.xls").Activate
Range("A6").Select

End Sub


It's finding a common, reproducible name for "Chart 43", since this changes each time I run the macro.

Many thanks.

Tom. :oops:
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Is it?

ActiveSheet.Shapes(ActiveChart.Name).ScaleWidth 0.67, msoFalse, msoScaleFromTopLeft
 

tmanger

Board Regular
Joined
Jul 15, 2002
Messages
101
Yes. That is a better way to clarify.

The full macro actually makes the chart, copies it to a userform. Once the userform is closed, the active chart is deleted.

Tom.
 

tmanger

Board Regular
Joined
Jul 15, 2002
Messages
101

ADVERTISEMENT

I'm sorry, I thought you were speaking generically.

Yes I did try:

ActiveSheet.Shapes(ActiveChart.Name).ScaleWidth 0.67, msoFalse, msoScaleFromTopLeft

and received a runtime error "Item with specified name wasn't found"

I did try this:

ActiveSheet.Shapes(1).ScaleWidth 0.67, msoFalse, isoScaleFromTopLeft

and it works, as long as the chart is the only shape on the sheet. Just didn't know if there was a way to name the shapes with VBA other than using an indexing system.

Tom.
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
The Shape has the name of the ChartObject containing the Chart, so try:

ActiveSheet.Shapes(ActiveChart.Parent.Name).ScaleWidth 0.67, msoFalse, msoScaleFromTopLeft
 

tmanger

Board Regular
Joined
Jul 15, 2002
Messages
101
Excellent!! Sometimes the simple things escape the my simple mind.

Thank you. (y)

Cheers,

Tom.
 
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,152,329
Messages
5,769,462
Members
425,551
Latest member
yeat

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