使用VBA参数化多个图表的图表大小

时间:2016-05-27 16:44:13

标签: vba charts

我正在构建一个包含多个图表的仪表板屏幕。我在各自的构建函数中声明了这些图表。

Dim chartGallonsComp as ChartObject
Set chartGallonsComp = wsDest.ChartObjects.Add(Left:=0, Top:=45, Width:=240, Height:=180)

由于我有多个图表,我想使用类似的东西将.Add方法参数传递给函数

Dim chartSalesComp As ChartObject, chartGallonsComp As ChartObject
Dim chartCustomers As ChartObject

Dim ws As Worksheet    
ws = Sheets("Dashboard")

Dim aryChartOneDimensions(), aryChartTwoDimensions()
Dim aryChartThreeDimensions()

aryChartOneDimensions = Array(0, 45, 240, 180)
aryChartTwoDimensions = Array(240, 45, 240, 180)
aryChartThreeDimensions = Array(480, 45, 240, 180)

With ws
    Set chartGallonsComp = ws.ChartObjects.Add(Left:=aryChartOneDimensions(1), Top:=aryChartOneDimensions(2),
                                        Width:=aryChartOneDimensions(3),
                                        , Height:=aryChartOneDimensions(4))
End With

我确定这是可行的,我只是无法正确使用语法。我尝试了很多方法,包括公式语法

Left:="=aryChartOneDimensions(1)"

但我的尝试都没有奏效。如果可以做到这一点,请告诉我。

1 个答案:

答案 0 :(得分:0)

Set chartGallonsComp = ws.ChartObjects.Add(Left:=aryChartOneDimensions(0), _
                                           Top:=aryChartOneDimensions(1), _
                                           Width:=aryChartOneDimensions(2), _
                                           Height:=aryChartOneDimensions(3))

请记住,数组默认情况下是从零开始的,而不是基于数组的(除非您在模块的顶部包含Option Base 1,并且我认为这不是一个好习惯)