问题描述
我正在尝试通过 VBA 设置 Excel 图表的大小,我注意到 .ChartArea.Width 和 .ChartArea.Height 属性设置的值通常与所需的值略有不同。例如。代码如下:
Sub test_chart_area()
Dim CA_w As Double
Dim CA_h As Double
CA_w = 500
CA_h = 300
With ActiveChart
.ChartArea.Width = CA_w
Debug.Print (".ChartArea.Width: " & .ChartArea.Width)
.ChartArea.Height = CA_h
Debug.Print (".ChartArea.Height: " & .ChartArea.Height)
End With
End Sub
将图表设置为“.ChartArea.Width: 496.902204724409”和“.ChartArea.Height: 293.152125984252”
.PlotArea.InsideWidth 和 .PlotArea.InsideHeight 属性,另一方面,显然只分配整数值。例如。代码如下:
Sub test_plot_area_inside()
Dim PA_iw As Double
Dim PA_ih As Double
PA_iw = 200.411968503937
PA_ih = 100.642677165354
With ActiveChart
.PlotArea.InsideWidth = PA_iw
Debug.Print (".PlotArea.InsideWidth: " & .PlotArea.InsideWidth)
.PlotArea.InsideHeight = PA_ih
Debug.Print (".PlotArea.InsideHeight: " & .PlotArea.InsideHeight)
End With
End Sub
将图表设置为“.PlotArea.InsideWidth: 200”和“.PlotArea.InsideHeight: 100”。
所需值和返回值之间的差异无关紧要,但我想了解这种行为的原因。
非常感谢。
解决方法
设置 .Width
的 .Height
和 ActiveChart.Parent
属性。