如何居中对齐调整大小的 PlotArea?
With objChart
.Left = myChtRange.Left
.Top = myChtRange.Top
.Width = myChtRange.Width
.Height = myChtRange.Height
With .Chart
'=====================
Dim temp As Integer
With .PlotArea
temp = .Width
temp = .Height
temp = .Left
temp = .Top
.Width = 200
.Height = 100
'.Left = 10
'.Top = 10
.Left = (objChart.Width - objChart.Chart.PlotArea.Width) / 2
.Top = (objChart.Height - objChart.Chart.PlotArea.Height) / 2
'.Position = xlChartElementPositionAutomatic
End With
'=====================
End With
End With
这不会使 PlotArea 居中对齐:
With .PlotArea
.Left = (objChart.Width - objChart.Chart.PlotArea.Width) / 2
这确实中心对齐PlotArea,但也重置其大小:
With .PlotArea
.Position = xlChartElementPositionAutomatic
如果绘图区域的大小受到右侧或下方可用空间的限制(高度/宽度可能最终小于您提供的值),则无法设置绘图区域的大小,因此首先设置顶部/左侧。
这对我有用:
Sub Tester()
Const H As Long = 250
Const W As Long = 250
Dim ws As Worksheet, co As ChartObject
Set ws = ThisWorkbook.Worksheets("chart")
Set co = ws.ChartObjects("Wykres2")
With co.Chart.PlotArea
.Left = (co.Width - W) / 2
.Top = (co.Height - H) / 2
.Width = W
.Height = H
End With
End Sub
...假设图表对象足够大。