Kir*_*met 6 vba charts trendlines microsoft-excel
有没有一种简单的方法可以将图表中的趋势线公式应用于 Excel 中的任何给定 X 值?
例如,我想获得给定 X = $2,006.00 的 Y 值。我已经采用了公式并将其重新输入为:
=-0.000000000008*X^3 - 0.00000001*X^2 + 0.0003*X - 0.0029
我通过添加更多数据不断对趋势线进行调整,并且不想每次都重新输入公式。

您可以编写一个 vba 用户定义函数来使用趋势线公式来评估给定的 x
这是一个入门示例
Function TrendLineValue(x As Double) As Double
Dim c As Chart
Dim t As Trendline
Dim s As String
' Get the trend line object
' this code assumes the first chart on the active sheet,
' and the first series, first trendline
Set c = ActiveSheet.ChartObjects(1).Chart
Set t = c.SeriesCollection(1).Trendlines(1)
' make sure equation is displayed
t.DisplayRSquared = False
t.DisplayEquation = True
' set number format to ensure accuracy
' adjust to suit requirements
t.DataLabel.NumberFormat = "0.0000E+00"
' get the equation
s = t.DataLabel.Text
' massage the equation string into form that will evaluate
' this code assumes 3rd order polynomial
s = Replace(s, "y =", "")
s = Replace(s, "x3", "x^3")
s = Replace(s, "x2", "x^2")
s = Replace(s, "x", " * " & x & " ")
' evaluate for given x value
TrendLineValue = Evaluate(s)
End Function
Run Code Online (Sandbox Code Playgroud)