我收到此运行时错误 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
请帮忙!