使用趋势线公式在 Excel 中获取任何给定 X 的值

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

我通过添加更多数据不断对趋势线进行调整,并且不想每次都重新输入公式。

在此处输入图片说明

bre*_*tdj 5

你可以用一个简单的LINEST公式来解决这个问题(没有图表)

对于三阶数组,输入
=LINEST(C2:C15,B2:B15^{1,2,3})
四个水平单元格以获得方程系数(aX^3+bX^2+cX+D),然后只需替换 X

LINEST对于更复杂的回归,这里有介绍

  • 我不想投票否决趋势线公式 vba 答案,但我想说 LINEST 比 VBA 方法容易得多,因为它直接使用计算,而不是可能没有格式化的公式以获得足够的准确性(参见 W_Whalley 的早期注释:使用 0.000000000000E+00 的数字格式有助于提高趋势线公式的准确性)。 (2认同)

chr*_*sen 4

您可以编写一个 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)