首页 > 解决方案 > 为什么无法通过 VBA 精确设置 Excel 图表的大小?

问题描述

我正在尝试通过 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”。

所需值和返回值之间的差异无关紧要,但我想了解这种行为的原因。

提前谢谢了。

标签: excelvbaexcel-charts

解决方案


设置 的.Width.Height属性ActiveChart.Parent


推荐阅读