Trying to center align a resized Plot Area in a resized Chart Area.
1. CHART AREA
The Chart Area's Border Line Weight = 6.
So I order to have the Chart Area cover the exact selected cell range horizontally, I resized the Chart Area.
I did this by counting the Pixels from Left, with the cells 65 Pixels wide:
' ChartArea Position
' Margin Left: 65 Px
objChart.Left = 68 ' =65+3(LineWeight/2)
' ChartArea Width
' Width: 2x65 = 130
objChart.Width = 124 ' =130-6(LineWeight)
2. PLOT AREA
Found this code by Andy Pope:
https://www.excelbanter.com/charts-c...plot-area.html
Sub x()
With ActiveChart
' center align plotarea in chart object
' .PlotArea.Left = (.ChartArea.Width - .PlotArea.Width) / 2
' .PlotArea.Top = (.ChartArea.Height - .PlotArea.Height) / 2
.Legend.Left = .PlotArea.InsideLeft + ((.PlotArea.InsideWidth -
..Legend.Width) / 2)
End With
End Sub
I want the Plot Area to be as wide as possible (as wide as the Chart Area allows) and place it in the Chart Area's Center.
So I added these lines, using my Pixelcount from the resized Chart Area:
' PlotArea Resize
.ChartArea.Select
.PlotArea.Select
Selection.Height = 200
Selection.Width = 112 ' =124-6-6=112 <<< 2x65 Pixels - 2x6Pixels = 112 Pixels
' Selection.Width = (.ChartArea.Width)
' Selection.Left = 0
' Selection.Top = 0
Selection.Left = (.ChartArea.Width + .ChartArea.Border.Weight - .PlotArea.Width) / 2
Selection.Top = (.ChartArea.Height - .PlotArea.Height) / 2
Somehow, this doesn't add up, the numbers don't work. The Plot Area is not centered.
It seems, that the resized Chart Area causes the irritation.
So I removed "Plot Resize" from the "Create" Macro, recorded another one to have some values
and finally ran these recorded values in a second Macro:
Sub Resize_PlotArea_xx()
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartArea.Select
ActiveChart.PlotArea.Select
' Selection.Height = 200
' Selection.Top = 20
Selection.Left = -1 ' RECORDED
Selection.Width = 117.965 ' RECORDED
' Selection.Left = (.ChartArea.Width - .PlotArea.Width) / 2
' Selection.Top = (.ChartArea.Height - .PlotArea.Height) / 2
End Sub
This seems to do the job, more or less, at Zoom 400, you can see, it's not exact.
And I have to record values by eyeballing and run a second Macro.
How can I make this work in one run? Without using the mouse?
(This is what I'm really starting to like about VBA: not using the mouse.)
And if that's not possible, how can I make these two lines work in the second Macro:
(Couldn't work out, what to add at the beginning, to make it work by itself.)
' Selection.Left = (.ChartArea.Width - .PlotArea.Width) / 2
' Selection.Top = (.ChartArea.Height - .PlotArea.Height) / 2
Thanks a lot!
Toni
Bookmarks