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

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
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.
 
Upvote 0
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.
 
Upvote 0
The Shape has the name of the ChartObject containing the Chart, so try:

ActiveSheet.Shapes(ActiveChart.Parent.Name).ScaleWidth 0.67, msoFalse, msoScaleFromTopLeft
 
Upvote 0
Excellent!! Sometimes the simple things escape the my simple mind.

Thank you. (y)

Cheers,

Tom.
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,239
Members
448,555
Latest member
RobertJones1986

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