Home > Enterprise >  Change chart height without resizing plotArea
Change chart height without resizing plotArea

Time:02-16

I am trying to resize an excel chart with VBA.

Is it possible to change ActiveChart.ChartArea.Height without affecting the size of the PlotArea? Whenever I attempt to change the chart height it looks like the plot area is automatically resized, which is an undesired outcome.

I have tried the following sequence, when downsizing a graph:

  1. Changing plotarea to fixed desired height;
  2. Changing chart height to fixed desired height;
  3. Changing plotarea to fixed desired height;

This sequence does not yield expected results, as (1) the chart is not changed to specified height, and (2) the plotarea height is output correctly, but its positioning within the chart (.InsideTop) has changed.

CodePudding user response:

Please, test the next way of dealing with a chart dimensions. The scenario involves the next process: firstly memorizing the PlotArea dimensions (Height/Width), then play with the chart (Object) dimensions, reset the PlotArea ones and set its Position to Automatic. Excel tries guessing what you want accomplishing and it looks/is more probable that both chart elements to be modified proportionally:

Sub testActiveChartDimensions()
  Dim ch As Chart, plHeight As Double, plWidth As Double
  Set ch = ActiveChart 'plays with a selectded chart

  plHeight = ch.PlotArea.height: plWidth = ch.PlotArea.width 'memorize the plot area dimensions

  ch.Parent.height = ch.Parent.height * 2: ch.Parent.width = ch.Parent.width * 2 'resize the chartObject
  
  ch.PlotArea.height = plHeight: ch.PlotArea.width = plWidth 'reset the initial dimensions for plot area
                                                             ' you can set any other dimensions (just to be lower than the new chart dimensions...)
  ch.PlotArea.Position = xlChartElementPositionAutomatic     'center it on the chart object
End Sub
  • Related