在循环中使用if语句会产生无效的参数错误

时间:2016-05-05 08:57:38

标签: excel vba excel-vba

我在Excel中编写了以下代码来创建XY散点图并格式化点,以创建在指定时间段内(sDateeDate之间的事件/决策的可视时间轴)

Option Explicit

Sub UpdateTimeline()

'Updates timeline chart with dates in specified range and updates formatting

    Dim timelineChart As Chart
    Dim recordCount As Long
    Dim record As Range

    Application.ScreenUpdating = False
    timelineSheet.Unprotect
    Call ClearSeries
    Set timelineChart = Worksheets("Timeline").ChartObjects("chtDecisionTimeline").Chart
    recordCount = 0
    For Each record In Range(decisionRecordSheet.Range("D7"), decisionRecordSheet.Range("D7").End(xlDown))
        recordCount = recordCount + 1
        If record.Value >= timelineSheet.Range("sDate") Then
            If record.Value <= timelineSheet.Range("eDate") Then
                timelineChart.SeriesCollection.NewSeries
                With timelineChart.SeriesCollection(recordCount)
                    .Name = "='Decision Record'!" & record.Offset(0, 1).Address
                    .XValues = "='Decision Record'!" & record.Address
                    .Values = "='Decision Record'!" & record.Offset(0, -2).Address
                    .AxisGroup = 2
                    .MarkerStyle = 8
                    .MarkerSize = 7
                    .MarkerBackgroundColor = RGB(228, 10, 56)
                    .MarkerForegroundColor = -2
                    .Format.Line.Visible = False
                    .ApplyDataLabels
                    .DataLabels.ShowValue = False
                    .DataLabels.ShowSeriesName = True
                    .DataLabels.Orientation = xlUpward
                        If record.Offset(0, -2).Value Mod 2 <> 0 Then
                            timelineChart.SeriesCollection(recordCount).DataLabels.Position = xlLabelPositionRight
                        Else
                            timelineChart.SeriesCollection(recordCount).DataLabels.Position = xlLabelPositionLeft
                        End If
                End With
            End If
        End If
    Next
    timelineChart.SetElement (msoElementSecondaryValueAxisNone)
    With timelineChart.Axes(xlCategory, xlPrimary)
        .MaximumScale = timelineSheet.Range("eDate").Value
        .MinimumScale = timelineSheet.Range("sDate").Value
    End With
    With timelineChart.Axes(xlValue, xlPrimary)
        .MaximumScale = lookupSheet.Range("yMax").Value
        .MinimumScale = lookupSheet.Range("yMin").Value
    End With
    With timelineSheet
        .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        .EnableSelection = xlUnlockedCells
    End With

    Application.ScreenUpdating = True

End Sub

decisionRecordSheet在B-F列中包含以下格式的数据:

Image showing layout of decisionRecordSheet data

时间线表格如下:

enter image description here

代码工作正常(虽然我知道它并不漂亮),除非用户指定的日期范围(sDateeDate)表示只需要绘制0-2条记录图表,在这种情况下,它会引发With timelineChart.SeriesCollection(recordCount)的无效参数错误。

我已经尝试将18,19,42和43行注释掉,并且if语句似乎减少了满足导致问题的条件的记录数量。我还尝试减少总数据集,以便只有0-2,我得到相同的错误。

当然,这可能与正在绘制的记录数无关,但这些是唯一一致复制此行为的测试。

修改

当绘制的项目超过2个时,我也会遇到运行时错误,但前提是我的代码中包含18,19,42和43行 - 如果我将它们注释掉,我就不会再出错了。

Error message

当我调试时,问题似乎是timelineChart.SetElement (msoElementSecondaryValueAxisNone),但我不明白为什么。

1 个答案:

答案 0 :(得分:0)

我不能复制它,因为你打电话给我们不在这里的代码,但是: -

如果您将timelineChart.SeriesCollection.NewSeries移至timelineChart.SeriesCollection(recordCount)之后,或更改

timelineChart.SeriesCollection(timelineChart.SeriesCollection.Count)更改为recordCount

这可能会带来积极的结果。您用于查找集合的if每次都会增加,但有一个{{1}}语句表示每次都可能无法生成系列文件。