-2

我收到此运行时错误 9,创建图表时 Excel VBA 2003 中的下标超出范围,

在代码中的某个地方,Public Chrt_color As Variant还有
'Assigning the chart colors Chrt_color = Array(4, 7, 9, 10, 11, 12, 13, 14, 17, 18, 21, 22, 23, 3, 43, 51, 50, 39, 47, 52, 56)

Public Sub label_creation_Chart5(ByRef wksht As Excel.Worksheet)


Dim i As Integer
Dim j As Integer
Dim iTemp As Integer
Dim cht_Num As Integer
Dim iTextBoxLoc As Integer
Dim dbTemp As Double
Dim vSeriesValues As Variant
Dim dbSeriesLastValue() As Double


Application.ScreenUpdating = False
cht_Num = 5

wksht.ChartObjects("Chart 5").Activate
wksht.ChartObjects("Chart 5").Select

Do While ActiveChart.TextBoxes.Count > 0
    ActiveChart.TextBoxes(1).Delete
Loop

If ActiveChart.SeriesCollection.Count < 1 Then GoTo Sub_end

ReDim dbSeriesLastValue(1 To ActiveChart.SeriesCollection.Count) As Double
ReDim iSeriesIndex(1 To ActiveChart.SeriesCollection.Count) As Integer

For i = 1 To ActiveChart.SeriesCollection.Count
    vSeriesValues = ActiveChart.SeriesCollection(i).Values

    If wksht.Range("AJ" & (i + 52)).Value = "Yes" Or _
       (prdName = "" And InStr(wksht.Range("A" & (i + 52)).Value, corpName) > 2) Then
        ActiveChart.SeriesCollection(i).Border.ColorIndex = 5
        wksht.Range("Z" & (i + 52) & ":AH" & (i + 52)).Font.ColorIndex = 5
    Else
        ActiveChart.SeriesCollection(i).Border.ColorIndex = Chrt_color(i)
        wksht.Range("Z" & (i + 52) & ":AH" & (i + 52)).Font.ColorIndex = Chrt_color(i)
    End If

    dbSeriesLastValue(i) = vSeriesValues(UBound(vSeriesValues, 1))
    iSeriesIndex(i) = i
    iTextBoxLoc = 12 + 202 * (1 - (vSeriesValues(UBound(vSeriesValues, 1)) / (ActiveChart.Axes(xlValue).MaximumScale - ActiveChart.Axes(xlValue).MinimumScale)))
    With ActiveChart.TextBoxes.Add(195, iTextBoxLoc, 100, 13)
        .AutoSize = True
        .Text = ActiveChart.SeriesCollection(i).Name
        With .Font
            .Name = "Arial"
            .Size = 7
            .ColorIndex = ActiveChart.SeriesCollection(i).Border.ColorIndex
        End With
    End With
Next i
For i = 1 To (ActiveChart.SeriesCollection.Count - 1)
    For j = i + 1 To ActiveChart.SeriesCollection.Count
        If dbSeriesLastValue(j) < dbSeriesLastValue(i) Then
            dbTemp = dbSeriesLastValue(j)
            dbSeriesLastValue(j) = dbSeriesLastValue(i)
            dbSeriesLastValue(i) = dbTemp
            iTemp = iSeriesIndex(j)
            iSeriesIndex(j) = iSeriesIndex(i)
            iSeriesIndex(i) = iTemp
        End If
    Next j
Next i

当错误发生时,我在中间窗口中显示的ActiveChart.SeriesCollection(i).Border.ColorIndex = Chrt_color(i) 值也出现错误。ActiveChart.SeriesCollection(i).Border.ColorIndex = -4105

请帮忙!

4

2 回答 2

1

当您收到错误消息时,我会查看 i 的值。您的代码将 i 从迭代到系列数。假设您有比 Chtr_color 的元素更多的系列,那么 Chrt_color(i) 不会评估超出范围。

另一个问题可能是系列索引从 1 变为 N,而数组索引从 0 变为 N-1。

于 2012-10-08T07:29:10.063 回答
0

查看原始问题和评论,一切看起来都很完美。我有几个建议和问题:

似乎 ActiveChart.SeriesCollection 确实包含一个元素

1.你不确定吗?您没有在监视窗口中查看吗?

*'分配图表颜色 Chrt_color = Array(4, 7, 9, 10, 11, 12, 13, 14, 17, 18, 21, 22, 23, 3, 43, 51, 50, 39, 47, 52, 56)*

2.这很可能不是原因,但请检查单引号是否处于活动状态,如果是,则此行将被注释掉,从而使数组为空。

于 2012-10-08T08:44:32.813 回答