Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel: retrieving a trendline equation by code

Posted on 1999-09-29
6
Medium Priority
?
1,260 Views
Last Modified: 2008-03-04
I have a simple graph, 2 dimensions x and y, y=f(x) but the equation/relation is unknown and I want it !
So I insert a trendline (polynomial level 6 to have the best match). Then I display the trendline equation on the graph.
If I test the equation, applying to the x value the equation, the output is completly out of range.. I tried several times, always wrong output. Either I do not know how to read the equation or it is wrong or incorrectly displayed.
Can I retrieve that equation by any other means such as a bit of code in Visual Basic ?
thanks
0
Comment
Question by:lilletof
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
cri earned 400 total points
ID: 2083927
VBA is a bit too much for testing, but it can be done. But first highlight the equation and copy it to clipboard. Paste it into a cell and make a formula out of it by removing the y and replacing the x with the cell. This way you prevent a typo.
0
 
LVL 13

Expert Comment

by:cri
ID: 2083929
As for VBA, will look it up, be back soon.
0
 
LVL 13

Expert Comment

by:cri
ID: 2083944
Code courtesy of VBoukhAr in question Q.10188253:

This one gets it as string only:

Sub GetTrend()
 Dim strLabel As String
 strLabel = ThisWorkbook.Charts(1).SeriesCollection(1).Trendlines(1).DataLabel.Text
 MsgBox strLabel
End Sub

This parses the retrieved string. It assumes that you have Chart on _separate_ sheet. Open VBA project (Alt+F11) and copy-paste code below. Then run it (F5). In last rows at first worksheet you'll see full equation and separate coefficients of X in correspondent power.

Sub EquProc()
  Dim tmpStr As String, tmpNum As String, tmpX As String
  Dim Power As Boolean, k As Integer, LastRow As Integer
  tmpStr = ThisWorkbook.Charts(1).SeriesCollection(1).Trendlines(1).DataLabel.Text
  LastRow = Worksheets(1).UsedRange.Row + Worksheets(1).UsedRange.Rows.Count
  Worksheets(1).Cells(LastRow + 1, 1) = tmpStr
  k = 1
  Power = False
  tmpNum = "" 
  Do Until Len(tmpStr) <= 0
    Select Case Left(tmpStr, 1)
      Case "0" To "9", ",", "-", "+", "E"
         If Not Power Then
           tmpNum = tmpNum & Left(tmpStr, 1)
         Else
           tmpX = tmpX & Left(tmpStr, 1)
         End If
      Case "x", "X"
         Power = True
         tmpX = "x"
      Case Else
         If Power Then
           Worksheets(1).Cells(LastRow + 2, k) = tmpX
           Worksheets(1).Cells(LastRow + 3, k) = CDbl(tmpNum)
           tmpNum = "" 
           tmpX = "const"
           Power = False
           k = k + 1
         End If
      End Select
      tmpStr = Right(tmpStr, Len(tmpStr) - 1)
    Loop
    Worksheets(1).Cells(LastRow + 2, k) = tmpX
    Worksheets(1).Cells(LastRow + 3, k) = CDbl(tmpNum)
End Sub
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:ture
ID: 2084000
lilletof,

The cause of this is that the trendline equation is displayed with too low precision. Do this:

1. Create a chart and add a trendline (you know how to do that)
2. Right-click the trendline equation and do 'Format Datalabels'
3. On the 'Number' tab, choose category 'Number' and increase the decimal places to 8 or something.

Now you can use the values displayed in the trendline equation. It should work a lot better.

Ture Magnusson
Karlstad, Sweden
0
 
LVL 13

Expert Comment

by:cri
ID: 2084373
ture, this _could_ be the cause, but as liletof wrote completely out of range s/he must coefficients with only 1-2 significant digits. In my opinion a typo or wrong sign is more likely.  
0
 
LVL 22

Expert Comment

by:ture
ID: 2084482
Cri,
Perhaps you are right, perhaps not... I've faced this particular problem before and in that case it was a precision problem.
/Ture
0

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question