Automatically adjusting chart height VBA

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
256
Hey,

I created a 2d column graph that’s dynamic based up on a drop down list. My only problem with it is each individual graph looks completely different as the height I believe is fixed. What I need is for it to be dynamic so it doesn’t try to scale to said height adjustments. Having a fixed width is perfectly fine..

For example, graph 1 may have only 2 entries on the vertical axis so this looks huge if it’s fixed to B2:AA50.. then graph 2 may have 20 entries on the vertical axis so it will look small and unreadable as it fits to B2:AA50. What I’m thinking of is for each entry it has a fixed height and then for every entry it cumulates this but I’m unsure how to execute this as never worked with charts before.

Any help is appreciated :)
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hey

The code below will adjust chart height and width based on its data:


Code:
Sub Height_Width()
Dim cho As ChartObject, r As Range


For Each cho In ActiveSheet.ChartObjects
    Select Case cho.Chart.ChartType
        
        Case 51     ' vertical columns
            Set r = [a1].Resize(, cho.Chart.SeriesCollection(1).Points.Count)
            cho.Width = r.Width
            Set r = [a1].Resize(cho.Chart.Axes(xlValue).MaximumScale / 5)
            cho.Height = r.Height
        
        Case 57     ' horizontal bars
            Set r = [a1].Resize(cho.Chart.SeriesCollection(1).Points.Count * 5)
            cho.Height = r.Height
            Set r = [a1].Resize(, cho.Chart.Axes(xlValue).MaximumScale / 5)
            cho.Width = r.Width
    End Select
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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