Excel VBA For循环错误450在图表的系列集合创建中
我是VBA新手,我想为图表定义系列。 这是密码Excel VBA For循环错误450在图表的系列集合创建中,excel,vba,loops,excel-charts,Excel,Vba,Loops,Excel Charts,我是VBA新手,我想为图表定义系列。 这是密码 For i = 2 To lsrw (last row in my code) 'Define the series for the chart in case of empty series ActiveChart.SeriesCollection.NewSeries ActiveChart.SeriesCollection(sc).Name = sh.Range("J" & i).Value
For i = 2 To lsrw (last row in my code)
'Define the series for the chart in case of empty series
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(sc).Name = sh.Range("J" & i).Value
ActiveChart.SeriesCollection(sc).XValues = _
Array(sh.Range("A" & i), sh.Range("A" & i))
ActiveChart.SeriesCollection(sc).Values = _
Array(sh.Range("K" & i), sh.Range("L" & i))
sc=sc+1
Next
我想要的图表是一张零散的。
这些线是用来画垂直线的。
它与“下一步错误恢复”一起工作,但之后我无法添加新系列。当没有更多的系列要创建时,代码似乎不会停止。我不明白什么不起作用
问题是,我的数据集的大小会随着我在打印它之前执行SQL请求而不断变化
编辑:问题已解决,但现在它出现在其他地方(错误1004):
'For j = 3 To 20
'
' If sh.Cells(lstrow, j).Value <> 0 Then
' ActiveChart.SeriesCollection.NewSeries
' ActiveChart.SeriesCollection(sc).Name = sh.Cells(lstrow - 1, j).Value
'
' ActiveChart.SeriesCollection(sc).XValues = _
' Array(sh.Range("A2"), sh.Range("A" & lsrw))
'
' ActiveChart.SeriesCollection(sc).Values = _
' Array(sh.Cells(lstrow, j).Value, sh.Cells(lstrow, j).Value)
' End If
'
' sc = sc + 1
' ActiveChart.SeriesCollection(sf).AxisGroup = xlPrimary
'
'Next
您能显示数据的快照吗?还有哪一行导致了上述错误?问题在循环的末尾。我试着用ctrl+F8调试并运行每一行代码。每次都有效,但在绘制最后一个数据后,我返回了此错误
For Each chrt In sh.ChartObjects
chrt.chart.ChartArea.Font.Size = 9
chrt.chart.ChartArea.Font.Name = "Cambria"
chrt.chart.ChartArea.Border.LineStyle = xlNone
chrt.chart.Axes(xlValue).MinimumScale = 0
chrt.chart.Axes(xlValue).MaximumScale = yMax + 50
chrt.chart.Axes(xlCategory).MinimumScale = xMin - 1
chrt.chart.Axes(xlCategory).MaximumScale = xMax + 1
chrt.chart.HasAxis(xlCategory) = False
chrt.chart.Axes(xlCategory).Width = 400
chrt.chart.Axes(xlValue).HasTitle = True
chrt.chart.Axes(xlValue).AxisTitle.Text = "Depth in m"
Next chrt