Here's a way to cycle through the SeriesCollection
of a chart and extract the equation from the trend line associated with each Series
in it. If needed, you can change each instance of SeriesCollection
to FullSeriesCollection
.
The code
- checks if the series in question has a at least one
Trendline
- note there could be multiple. This code only deals with the first but could be easily modified to loop through multiple trend lines.
- checks if the
TrendLine
is displaying its equation.
- "Copies" the text of the equation to a specified
Range
. Here the Offset
is moving 1 column to the right for each successive trend line. On the first iteration, B56
is offset 1 column so that your equation appears in C56
.
Sub Equations()
Dim chrtObj As ChartObject
Dim i As Long
Set chrtObj = Sheets("Sheet1").ChartObjects("Chart 1") ' Change to your sheet name here
With chrtObj.Chart
For i = 1 To .SeriesCollection.Count
If .SeriesCollection(i).Trendlines.Count > 0 Then
With .SeriesCollection(i).Trendlines(1)
If .DisplayEquation Then
Sheets("Sheet1").Range("B56").Offset(0, i).Value = .DataLabel.Text ' Change sheet name here as well
End If
End With
End If
Next i
End With
End Sub
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…