Maurice,
I've never managed to do this absolutely perfectly thanks to the lack of
exactitude in the excel chart engine, but this gets very close. It assumes
you have a chart sheet set up with an existing chart on it, and have just
added another chart to the chart sheet.
If anyone can improve on this, please let me know directly. Thanks.
Sub AlignSubChartOnParent(chParent As Variant, chSubChart As Variant, _
Optional dSubHeight As Double)
'chParent is the chartsheet
'chSubChart is the sub chart
'aligns the sub chart underneath all the other charts on the page
'assumes no x-axis labels on the pre-existing charts
'kludges various items to make things work
Dim dPTotalWidth As Double
Dim dPCatHeight As Double
Dim dPCatWidth As Double
Dim dPValueLeft As Double
Dim lCounter As Long
Dim dAdjust As Double
Dim lVersion As Long
Dim shStart As Worksheet
Set shStart = ActiveSheet
lVersion = Val(Application.Version)
dAdjust = 0
If lVersion < 9 Then
chParent.Select
chSubChart.Parent.Select
chSubChart.Parent.Activate
chSubChart.PlotArea.Select
End If
'get parent dimensions
With chParent
dPTotalWidth = .ChartArea.Width
dPCatHeight = .Axes(xlValue).Top + .Axes(xlValue).Height
dPCatWidth = .Axes(xlCategory).Width
dPValueLeft = .Axes(xlValue).Left
End With
'get total height of all pre-existing charts on the sheet
For lCounter = 1 To chParent.ChartObjects.Count - 1
With chParent.ChartObjects(lCounter).Chart
dPCatHeight = dPCatHeight + .Axes(xlValue).Height
End With
Next lCounter
With chSubChart
.Parent.Left = 0
.Parent.Width = dPTotalWidth
If dSubHeight <> 0 Then .Parent.Height = dSubHeight
dSubHeight = .Parent.Height
'set the plot area narrower than necessary then expand it later
With .PlotArea
.Top = 0
.Width = dPTotalWidth - 150
.Height = dSubHeight
End With
'try and get the left edges to line up
.PlotArea.Left = .PlotArea.Left + dPValueLeft - _
.Axes(xlValue).Left - .ChartArea.Left - dAdjust
'readjust the width
.PlotArea.Width = .PlotArea.Width + dPCatWidth - .Axes(xlCategory).Width
+ dAdjust
'readjust the left edge
.PlotArea.Left = .PlotArea.Left + dPValueLeft - .Axes(xlValue).Left -
..ChartArea.Left
.Parent.Top = dPCatHeight - .Axes(xlValue).Top - .ChartArea.Top - 0.5
End With
shStart.Select
End Sub
Robin Hammond
www.enhanceddatasystems.com