Excel VBA 趋势线数据标签文本为空

Excel VBA Trendline Data Label Text is Empty

我大约一年前编写了这段代码,并且运行良好。然而,最近它坏了。我假设有一个 Windows 更新改变了 Excel 的宏处理图表的方式。我的代码的相关部分执行以下操作:

  1. 在 "prep" 工作表上创建 XY 散点图
  2. 为 XY 散点图上的每个数据系列创建趋势线
  3. 解析斜率和 R^2 的每个趋势线文本,将它们作为文本复制到另一个 "summary" 工作表

我发现第 1 步和第 2 步工作正常,但是当我尝试解析趋势线文本时(预期类似 y = 0.0289x + 143),我得到一个空字符串(参见注释掉的内容) MsgBox,如下)。更奇怪的是,当宏完成(或失败)时,图表更新并且文本显示正常。

' Create the graph for the linear part of the data set
Worksheets(PrepSheetName).Activate
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterSmooth
ActiveChart.SetSourceData Source:=Range("$B:$G")
' ActiveChart.Name = "Linear"
'MsgBox "Past linear creation"

' Add each data set to the chart individually
ActiveChart.PlotArea.Select
For i = 1 To 5

    ' Construct a string like ='Data'!$C:$C
    YValues = "='" & PrepSheetName & "'!$" & DSMeasCol(i) & "$"
    YValues = YValues & CStr(PrepDataStart) & ":$" & DSMeasCol(i) & "$"
    YValues = YValues & CStr(PrepDataEnd(i))

    ' Construct a string like ='Data'!$C:$C
    XValues = "='" & PrepSheetName & "'!$" & DSCmdCol(i) & "$"
    XValues = XValues & CStr(PrepDataStart) & ":$" & DSCmdCol(i) & "$"
    XValues = XValues & CStr(PrepDataEnd(i))

    ' Give the chart values
    ActiveChart.SeriesCollection(i).Values = YValues
    ActiveChart.SeriesCollection(i).XValues = XValues

    ' Create a trendline for the chart
    Dim TL As Trendline
    Set TL = ActiveChart.SeriesCollection(i).Trendlines.Add(Type:=xlLinear, Forward:=0, _
        Backward:=0, DisplayEquation:=1, DisplayRSquared:=0, _
        Name:="LC" & CStr(i) & " Trend")
    TL.DisplayEquation = True
    TL.DisplayRSquared = False

    ' Exract the trendline formula
    Dim Eqn As String
    TL.Select
    'MsgBox "Trendline Text: " + TL.DataLabel.Text

    Eqn = Split(TL.DataLabel.Text, "=")(1)
    ' ... and place it on the coversheet ...
    CoverSheet.Cells(CSResults(i), CSFitSlope).Value = Split(Eqn, "x")(0)
    CoverSheet.Cells(CSResults(i), CSFitOffset).Value = Split(Eqn, "x")(1)

    'Find the RSquared of the Trendline
    TL.DisplayEquation = False
    TL.DisplayRSquared = True
    TL.Select
    Eqn = TL.DataLabel.Text
    Eqn = Split(TL.DataLabel.Text, "=")(1)

    ' ... and place it on the coversheet ...
    'CoverSheet.Cells(CSResults(i), CSFitCorr).Value = Eqn

Next i

如果我在第一次 运行 启用宏后尝试 运行 诊断代码来解析 tredline 数据标签文本,它会看到文本。但是,当 运行 显示诊断代码时,我无法更改显示的趋势线数据的类型。例如,我希望如果我 运行:

 TL.DisplayEquation = True
 TL.DisplayRSquared = False
 MsgBox "Should show Equation."
 TL.DisplayEquation = False
 TL.DisplayRSquared = True
 MsgBox "Should show R^2."

... 我应该看到趋势线数据标签仅在第一个消息框出现时显示方程式,第二个消息框应冻结屏幕,以便仅显示 R^2。但是,当我 运行 编写这样的代码时,我发现我的假设是不正确的:数据标签保持冻结状态直到宏完成,即使 ScreenUpdating = True 似乎我的图表在宏时没有更新运行s,但只在最后更新。

我尝试在创建趋势线后放置 DoEventsApplication.Recalculate,但这只会导致我的 Excel 崩溃。添加 Application.ScreenUpdating = True 或 False 似乎也没有帮助...

有什么想法吗?我完全不知所措...

如果我没有提供足够的信息或有任何不清楚的地方,请告诉我。

谢谢!

我最终使用了 chillin 的建议并使用了 LinEst。根据 ,这似乎是 Excel 在宏执行期间如何处理图表数据标签的错误。总而言之,我的代码与上面几乎相同,但使用 LinEst 进行了以下更改,而不是解析 Trenline 的 DataLabel:

' Create a trendline for the chart
Dim TL As Trendline
Set TL = ActiveChart.SeriesCollection(i).Trendlines.Add(Type:=xlLinear, Forward:=0, _
Backward:=0, DisplayEquation:=1, DisplayRSquared:=0, _
Name:="LC" & CStr(i) & " Trend")      

' Generate the trendline constants and place them on the summary sheet
CoverSheet.Cells(CSResults(i), CSFitSlope).Value = "=INDEX(LINEST(" & YValues & "," & XValues & ",TRUE, TRUE), 1)"
CoverSheet.Cells(CSResults(i), CSFitOffset).Value = "=INDEX(LINEST(" & YValues & "," & XValues & ",TRUE, TRUE), 1,2)"
CoverSheet.Cells(CSResults(i), CSFitCorr).Value = "=INDEX(LINEST(" & YValues & "," & XValues & ",TRUE, TRUE), 3)"