Trevian
The following User Defined Function UDF will do what you want
Copy and paste it into a Code Module in VBA
To use it just enter =TrendY(value) in a cell
ie: =TrendY(20)
It is assuming that your chart is Chart1 and that there is only 1 trendline on the chart
The function works with all the trendline types and variants of those
If you don't have the Trendlines equation shown it will do that for you.
That is actually where the function gets the equation from in the first place.
Special Thanx to Daniel from http://www.excelhero.com/blogs/ for assistance with a memory leak and some code optimisation.
I have posted a working example of this at http://rapidshare.com/files/421807902/Trendline_Estimator.xlsb
It has been tested with Excel 2007 & 2010 an is ok, no promises on earlier versions
Function Trendy(ByVal XVal As Double) As Variant
'
' A Function to return a Y Value to a Charts Trendline equation
'
' Written by Ian Huitson
' Sept 2010
'
' Code improvements by Daniel Ferry
' http://www.excelhero.com/blogs/
'
Dim m As Double, a As Double, b As Double, c As Double
Dim num As Double, tval As Double, movav As Double
Dim myTrend As String
Dim lngPtr As Long
Const F = 5 'Formula Start position in strings
Const EXPO = 5
Const LINEAR = -4132
Const LOGO = -4133
Const POLYNOMIAL = 3
Const POWER = 4
DoEvents
DoEvents
With Sheet1.ChartObjects(1).Chart.SeriesCollection(1).Trendlines(1)
.DisplayEquation = True
.DataLabel.NumberFormat = "#,##0.0000"
myTrend = .DataLabel.Text
If .Type = EXPO Then
'y = 66.983e0.0107x
lngPtr = InStr(F, myTrend, "e"
a = Mid(myTrend, F, lngPtr - F)
b = Mid(myTrend, lngPtr + 1, Len(myTrend) - lngPtr - 1)
Trendy = a * EXP(b * XVal)
ElseIf .Type = LINEAR Then
'y = 1.0562x + 66.273
lngPtr = InStr(F, myTrend, "x"
m = Mid(myTrend, F, lngPtr - F)
c = Right(myTrend, Len(myTrend) - lngPtr - 2)
Trendy = XVal * m + c
ElseIf .Type = LOGO Then
'y = 14.195ln(x) + 48.775
a = Mid(myTrend, F, InStr(F, myTrend, "ln"
- F)
b = Right(myTrend, Len(myTrend) - InStr(F, myTrend, " + "
- 2)
Trendy = a * LOG(XVal) + b
ElseIf .Type = POLYNOMIAL Then
'y = -0.0534x2 + 3.2448x + 50.953
'with up to power 6
'y = 5E-07x6 - 1E-05x5 - 0.0016x4 + 0.0792x3 - 1.1071x2 + 5.4142x + 61.542
polyorder = .Order
'Increase decimals to improve accuracy
.DataLabel.NumberFormat = "#,##0.000000000000"
DoEvents 'Updates Equation Textbox on screen
'get formula for Polynomial equation
myTrend = .DataLabel.Text
'Reset decimals for Display
.DataLabel.NumberFormat = "#,##0.0000"
DoEvents
tval = 0 'temporary counter
If InStr(1, myTrend, "x6"
> 0 Then
lngPtr = InStr(1, myTrend, "="
num = Mid(myTrend, lngPtr + 1, InStr(1, myTrend, "x6"
- lngPtr - 1)
tval = tval + num * (XVal ^ 6)
End If
If InStr(1, myTrend, "x5"
> 0 Then
If polyorder = 5 Then
lngPtr = InStr(1, myTrend, "="
num = Mid(myTrend, lngPtr + 1, InStr(1, myTrend, "x5"
- lngPtr - 1)
Else
lngPtr = InStr(1, myTrend, "x6"
num = Mid(myTrend, lngPtr + 2, InStr(1, myTrend, "x5"
- lngPtr - 2)
End If
tval = tval + num * (XVal ^ 5)
End If
If InStr(1, myTrend, "x4"
> 0 Then
If polyorder = 4 Then
lngPtr = InStr(1, myTrend, "="
num = Mid(myTrend, lngPtr + 1, InStr(1, myTrend, "x4"
- lngPtr - 1)
Else
lngPtr = InStr(1, myTrend, "x5"
num = Mid(myTrend, lngPtr + 2, InStr(1, myTrend, "x4"
- lngPtr - 2)
End If
tval = tval + num * (XVal ^ 4)
End If
If InStr(1, myTrend, "x3"
> 0 Then
If polyorder = 3 Then
lngPtr = InStr(1, myTrend, "="
num = Mid(myTrend, lngPtr + 1, InStr(1, myTrend, "x3"
- lngPtr - 1)
Else
lngPtr = InStr(1, myTrend, "x4"
num = Mid(myTrend, lngPtr + 2, InStr(1, myTrend, "x3"
- lngPtr - 2)
End If
tval = tval + num * (XVal ^ 3)
End If
If InStr(1, myTrend, "x2"
> 0 Then
If polyorder = 2 Then
lngPtr = InStr(1, myTrend, "="
num = Mid(myTrend, lngPtr + 1, InStr(1, myTrend, "x2"
- lngPtr - 1)
Else
lngPtr = InStr(1, myTrend, "x3"
num = Mid(myTrend, lngPtr + 2, InStr(1, myTrend, "x2"
- lngPtr - 2)
End If
tval = tval + num * (XVal ^ 2)
End If
If InStr(1, myTrend, "x + "
> 0 Then
lngPtr = InStr(1, myTrend, "x2"
num = Mid(myTrend, lngPtr + 2, InStr(1, myTrend, "x + "
- lngPtr - 2)
tval = tval + num * XVal
tval = tval + Right(myTrend, Len(myTrend) - InStr(1, myTrend, "x + "
)
End If
If InStr(1, myTrend, "x - "
> 0 Then
lngPtr = InStr(1, myTrend, "x - "
num = Mid(myTrend, InStr(1, myTrend, "x2"
+ 2, lngPtr - InStr(1, myTrend, "x2"
- 2)
tval = tval + num * XVal
tval = tval + Right(myTrend, Len(myTrend) - lngPtr)
End If
Trendy = tval
ElseIf .Type = POWER Then
'y = 55.426x0.1479
lngPtr = InStr(F, myTrend, "x"
a = Mid(myTrend, F, lngPtr - F)
b = Right(myTrend, Len(myTrend) - lngPtr)
Trendy = a * (XVal ^ b)
Else
MoveAv = .Period
If MoveAv <= [B43] Then
Trendy = Application.Evaluate("sum(offset(C1,B43,,-" & MoveAv & "
)"
/ MoveAv
Else
Trendy = "Can't calc"
End If
End If
End With
End Function