当使用数组时,“下标超出范围”

我是VBA的新手,在尝试使用数组来计算数字列表中的最大值时遇到错误9问题“下标超出范围”。 我有一个横跨几个团队(作为X轴值)和类别(作为系列)的堆积条形图。 对于每个团队,每个类别都有数字(事件标签状态),并且我想要find哪个团队的票数最多,并使用该信息来格式化我的条形图。 有问题的代码行突出显示。 我已经阅读了许多关于错误9和VBAarrays的线程,但问题仍然存在。 任何人都可以给我一个关于我的代码出了什么问题的提示吗? 我将不胜感激。

Sub ChangeByTeamChartColor() Workbooks.Application.ActiveWorkbook.Sheets("By Team - Incident State").Select ActiveSheet.ChartObjects("By Team Chart").Select Dim s As Series Dim teamTotal() As Integer Dim seriesCount As Integer, seriesIterator As Integer, pointIterater As Integer **ReDim teamTotal(1 To ActiveChart.SeriesCollection(1).Points.Count)** 'Iterate through series and set colors For seriesIterator = 1 To ActiveChart.SeriesCollection.Count Set s = ActiveChart.SeriesCollection(seriesIterator) If s.Name = "Active >24 h" Or s.Name = "Active" Then s.Format.Fill.ForeColor.RGB = RGB(192, 80, 77) 'Red Accent 2 Light 80 ElseIf s.Name = "Active <24 h" Or s.Name = "New" Then s.Format.Fill.ForeColor.RGB = RGB(247, 150, 70) 'Orange ElseIf s.Name = "Pending Customer" Then s.Format.Fill.ForeColor.RGB = RGB(79, 129, 189) 'Blue ElseIf s.Name = "Pending Vendor" Then s.Format.Fill.ForeColor.RGB = RGB(128, 100, 162) 'Purple ElseIf s.Name = "Scheduled" Then s.Format.Fill.ForeColor.RGB = RGB(155, 187, 89) 'Green ElseIf s.Name = "Closed" Or s.Name = "Resolved" Then s.Format.Fill.ForeColor.RGB = RGB(148, 138, 84) 'Brown ElseIf s.Name = "Unassigned" Then s.Format.Fill.ForeColor.RGB = RGB(255, 192, 0) 'Yellow End If 'Find the "Grand Total" datapoint in each series and hide it For pointIterater = 1 To s.Points.Count If s.XValues(pointIterater) = "Grand Total" Then s.Points(pointIterater).Interior.ColorIndex = xlNone End If ' The following line gives the error =================================== teamTotal(pointIterator) = teamTotal(pointIterator) + s.Values(pointIterator) ' ======================================================================== Next pointIterater Next seriesIterator 

结束小组

数组是零索引的(默认情况下 – 见下面的注释)。 第一个元素是“0”,当通过.Count循环到一个集合的结尾时,你必须小心。 你想在.Count – 1结束它

改变你的代码是这样的:

 For seriesIterator = 0 To ActiveChart.SeriesCollection.Count - 1 Set s = ActiveChart.SeriesCollection(seriesIterator) If s.Name = "Active >24 h" Or s.Name = "Active" Then s.Format.Fill.ForeColor.RGB = RGB(192, 80, 77) 'Red Accent 2 Light 80 ElseIf s.Name = "Active <24 h" Or s.Name = "New" Then s.Format.Fill.ForeColor.RGB = RGB(247, 150, 70) 'Orange ElseIf s.Name = "Pending Customer" Then s.Format.Fill.ForeColor.RGB = RGB(79, 129, 189) 'Blue ElseIf s.Name = "Pending Vendor" Then s.Format.Fill.ForeColor.RGB = RGB(128, 100, 162) 'Purple ElseIf s.Name = "Scheduled" Then s.Format.Fill.ForeColor.RGB = RGB(155, 187, 89) 'Green ElseIf s.Name = "Closed" Or s.Name = "Resolved" Then s.Format.Fill.ForeColor.RGB = RGB(148, 138, 84) 'Brown ElseIf s.Name = "Unassigned" Then s.Format.Fill.ForeColor.RGB = RGB(255, 192, 0) 'Yellow End If 'Find the "Grand Total" datapoint in each series and hide it For pointIterater = 0 To s.Points.Count - 1 If s.XValues(pointIterater) = "Grand Total" Then s.Points(pointIterater).Interior.ColorIndex = xlNone End If teamTotal(pointIterator) = teamTotal(pointIterator) + s.Values(pointIterator) Next pointIterater Next seriesIterator