VBA:根据系列集合自动调整所有y轴范围

时间:2016-08-25 00:39:25

标签: excel vba excel-vba

我正在尝试根据每个特定图表中所有系列的最大值和最小值(大于1)更新所有图表的y轴。 (因此,所有图表将根据其中的系列进行不同的更新)。我怎样才能做到这一点?据我所知,没有简单的SeriesCollection(all)选项,所以我试图使用循环。欢迎任何更好的建议。

Public Sub UpdateChartAxes()

Dim objChart As ChartObject
For Each objChart In Sheets("Charts").ChartObjects
    UpdateChartAxis objChart
Next objChart

End Sub

Private Sub UpdateChartAxis(ByVal objChart As ChartObject)
On Error GoTo CleanFail

Dim lower As Double
Dim upper As Double
Dim srs As Series

lower = 100
upper = 0

With objChart.Chart
For Each srs In .SeriesCollection
If Application.RoundDown(Application.Aggregate(15, 6, srs, 1), 0) < lower _
    Then lower = Application.RoundDown(Application.Aggregate(15, 6, srs, 1), 0)

If Application.RoundUp(Application.Aggregate(14, 6, srs, 1), 0) > upper _
    Then upper = Application.RoundUp(Application.Aggregate(14, 6, srs, 1), 0)
Next srs

.Axes(xlValue).MinimumScale = lower
.Axes(xlValue).MaximumScale = upper
End With

CleanExit:
Exit Sub

CleanFail:
Debug.Print lower
Debug.Print "Failed updating axes for chart '" & objChart.Name & "'.Message: " & Err.Description
Resume CleanExit
End Sub

1 个答案:

答案 0 :(得分:0)

问题是for循环中的“srs”(Aggregate不接受系列对象)。请改用“srs.Values”。