【问题标题】:VBA - trendline equation only available when stepping through, not runningVBA - 趋势线方程仅在单步执行时可用,而不是运行
【发布时间】:2020-10-28 18:27:48
【问题描述】:

我正在尝试从活动图表中的每条趋势线构建斜率collection。使用 F8 单步执行脚本时,我可以提取趋势线方程,但是当我删除断点并使用 F5 运行时,s.Trendlines(1).DataLabel.Text 只会返回一个空字符串到strEquations。这是一个非常可重复的错误,我可以单步执行所有序列并提取所有方程,或者单步执行第一个趋势线,然后运行其余部分,脚本将在第二个趋势线上中断,Run-time error 9. Subscript out of range 在行 afterEquals = Split(upToX(0), " ")

这是我在活动图表上运行的脚本:

ThemeColor = 5 'msoThemeColorAccent enumeration starts at 5
Set trendEqns = New Collection
For Each s In ActiveChart.SeriesCollection
    'Debug.Print s.Name                         'debug print which series 's' is being manipulated
    s.Trendlines.Add                            'adds a trendline to series (s)
    s.Trendlines(1).Select
    s.Trendlines(1).DisplayEquation = True      'displays the equation for each new trendline
    s.Trendlines(1).DataLabel.Format.TextFrame2.TextRange.Font.Fill.ForeColor.ObjectThemeColor = ThemeColor

    'THIS WORKS WHEN STEPPING THROUGH, but NOT RUNNING???
    strEquation = s.Trendlines(1).DataLabel.Text    'strEquation = "y = m*x + b"
    upToX = Split(strEquation, "x")                 'upToX = "y = m"
    afterEquals = Split(upToX(0), " ")              'afterEquals = "m"
    Slope = afterEquals(2)                          'Slope = "m"
    'Debug.Print Slope
    trendEqns.Add Slope
    ThemeColor = ThemeColor + 1
Next s

【问题讨论】:

  • 我讨厌 VBA。有很多次我在运行时遇到问题,但单步执行会产生预期的结果。正如 Tim 所说,有时它需要“赶上”,我什至使用 sleep 在某些函数的每个循环之间等待 x 秒,以试图避免这种情况发生。

标签: vba


【解决方案1】:

尝试使用DoEvents 添加一个循环,以允许 Excel “赶上”您的代码。在循环中,您可以检查等式的值并在它非空时退出(如果已经过了某个最大时间,这样您就不会永远卡在循环中)

ThemeColor = 5 'msoThemeColorAccent enumeration starts at 5
Set trendEqns = New Collection
For Each s In ActiveChart.SeriesCollection
    'Debug.Print s.Name                         'debug print which series 's' is being manipulated
    s.Trendlines.Add                            'adds a trendline to series (s)
    s.Trendlines(1).Select
    s.Trendlines(1).DisplayEquation = True      'displays the equation for each new trendline
    s.Trendlines(1).DataLabel.Format.TextFrame2.TextRange.Font.Fill.ForeColor.ObjectThemeColor = ThemeColor

    strEquation = ""
    t = Timer 'set timepoint
    Do
        DoEvents
        strEquation = s.Trendlines(1).DataLabel.Text    'strEquation = "y = m*x + b"
    Loop While strEquation = "" And (Timer - t) < 1
    
    upToX = Split(strEquation, "x")                 'upToX = "y = m"
    afterEquals = Split(upToX(0), " ")              'afterEquals = "m"
    Slope = afterEquals(2)                          'Slope = "m"
    Debug.Print Slope
    trendEqns.Add Slope
    ThemeColor = ThemeColor + 1
Next s

【讨论】:

  • 这非常有效。我猜想在跑步(而不是踩踏)时需要时间赶上并试图想出一种延迟的方法,但还没有想出那么聪明的东西。我也不想每次都延迟 X 秒,以防止根据图形/系列的数量线性增加运行时间,特别是如果我的 X 秒不够长并且偶尔会中断。感谢您的修复!
【解决方案2】:

据我所知,VBA 是面向对象的编程,有些对象是对另一个对象的特定重新定义,该对象调用Parent对该对象(有一个层次顺序)。趋势线的情况是图表中的“子”对象。因此,为了调用趋势线属性,并返回分层步骤,使用应用程序Parent 让代码知道您要激活趋势线属性非常重要。在 F8 模式下,我认为代码知道您需要趋势线的属性,但不是 F5 模式。

试试nameofyourchart.Parent.Activate,它会起作用的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 2018-09-12
    • 2013-05-08
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多