【问题标题】:"Subscript out of range" When Using Array使用数组时“下标超出范围”
【发布时间】:2016-03-22 20:14:41
【问题描述】:

我是 VBA 新手,在尝试使用数组计算数字列表中的最大值时遇到错误 9 问题“下标超出范围”。我有一个跨越多个团队(作为 x 轴值)和类别(作为系列)的堆叠条形图。对于每个团队,每个类别都有数字(事故工单状态),我想找出哪个团队的工单数量最多,并使用该信息来格式化我的条形图。突出显示有问题的代码行。我已经阅读了许多关于错误 9 和 VBA 数组的先前线程,但问题仍然存在。谁能给我提示一下我的代码出了什么问题?我将不胜感激。

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 结束它
  • "有问题的代码行被突出显示。"
  • @roryap 我假设它是从底部算起的第三行,带有两个星号,以及 For 循环之前数组的 ReDim。
  • 两个字:Option Explicit。您已声明 pointIteratEr,但随后使用 pointIteratOr 作为索引。

标签: vba excel


【解决方案1】:

数组是零索引的(默认情况下——见下面的 cmets)。第一个元素是“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

【讨论】:

  • 如果您将数组声明为1 To x,则数组不是零索引。
  • 我在使用 VB 的 15 年中从未听说过。可以提供参考吗?
  • VBA 语言规范。它部分声明,“如果存在 ,则其 提供对应维度的下限 Long 数据值。”坦率地说,我很惊讶你已经 15 年没有遇到过这种情况了。
  • 在那段时间里,我 95% 的“VB”编码都是在 VB.NET 中完成的,其中the lower bound for arrays is always zero。 VBA(或者可能是其他风格的 VB)允许零以外的东西这一事实是可以理解的在洗牌中丢失的那些古怪和深奥的功能之一。
  • only VB.Net 不允许这样做。 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-04
  • 1970-01-01
  • 2020-05-03
相关资源
最近更新 更多