gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:00:43 25 4
gpt4 key购买 nike

我在 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

而timelineSheet设置如下:

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) 上。但我不知道为什么。

最佳答案

当您调用我们这里没有的代码时,我无法复制它,但是:-

如果你搬家recordCount = recordCount + 1追随timelineChart.SeriesCollection.NewSeries , 或改变

或者

更改timelineChart.SeriesCollection(recordCount)timelineChart.SeriesCollection(timelineChart.SeriesCollection.Count)
这可以提供积极的结果。您的 recordCount用于查找集合的数据每次都会增加,但有一个 if这意味着可能不会每次都制作一个系列的声明。

关于vba - 在循环中使用 if 语句会产生无效参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37046407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com