Solved

Excel Interpet Statistical Function converted to VB6

Posted on 2011-02-22
6
429 Views
Last Modified: 2013-11-26
I have and Excel spread sheet that has cells that containing the following values:
Known Y   Known X
2                6
3                5
9                11
1                7
8                5

In a cell I use the INTERCEPT function which Calculates the point at which a line will intersect the y-axes by using a best fit regression line plotted through the known x-values and y-values.

I need to do this in VB6 can you update the code below so that it will calculate the line of Intercept?

When it works correctly the Intercept value should equal 0.048387

Thanks
Public Function Intercept() As Double
  
  Dim KnownY(1 To 5) As Double
  Dim KnownX(1 To 5) As Double
  
  KnownY(1) = 2
  KnownY(2) = 3
  KnownY(3) = 9
  KnownY(4) = 1
  KnownY(5) = 8
  
  KnownX(1) = 6
  KnownX(2) = 5
  KnownX(3) = 11
  KnownX(4) = 7
  KnownX(5) = 5
  
  Intercept =

End Function

Open in new window

0
Comment
Question by:xsawkins
[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
  • 3
6 Comments
 
LVL 5

Accepted Solution

by:
roger_karam earned 500 total points
ID: 34957128
I don't know if you can import the functions into regular VB, so here is the math solution for you following excel's explanation for the intercept function. You could also make it so it takes larger fields (more than 5 knowns) but this will do for your example:

Public Function Intercept() As Double
 
 Dim KnownY(1 To 5) As Double
  Dim KnownX(1 To 5) As Double
  Intercept = 0
 
  KnownY(1) = 2
  KnownY(2) = 3
  KnownY(3) = 9
  KnownY(4) = 1
  KnownY(5) = 8
 
  KnownX(1) = 6
  KnownX(2) = 5
  KnownX(3) = 11
  KnownX(4) = 7
  KnownX(5) = 5
 
  Dim dAverageY As Double
  Dim dAverageX As Double
  Dim dSlope As Double
  Dim dSlopeTop As Double
  Dim dSlopeBottom As Double
 
 
  dAverageY = 0
  dAverageX = 0
  dSlope = 0
 
  For i = 1 To 5
 
  dAverageY = dAverageY + KnownY(i)
  dAverageX = dAverageX + KnownX(i)
 
  Next i
 
  dAverageY = dAverageY / 5
  dAverageX = dAverageX / 5
 
 
  For i = 1 To 5
 
  dSlopeTop = dSlopeTop + ((dAverageX - KnownX(i)) * (dAverageY - KnownY(i)))
  dSlopeBottom = dSlopeBottom + (dAverageX - KnownX(i)) ^ 2
   
  Next i
 
  dSlope = dSlopeTop / dSlopeBottom
 
  For i = 1 To 5
 
  Intercept = Intercept + KnownY(i) - dSlope * KnownX(i)
   
  Next i
 
  Intercept = Intercept / 5

End Function
0
 
LVL 5

Expert Comment

by:roger_karam
ID: 34957164
oh, my bad. if you are using VB 6, you need to switch the "Next i" statements for just "loop"

-RK
0
 
LVL 5

Expert Comment

by:roger_karam
ID: 34957637
of course, if i totally understood you wrong and you are doing this in vba, just use:

intercept = Application.WorksheetFunction.Intercept(KnownY(), KnownX)
0
 

Author Comment

by:xsawkins
ID: 34964849
Thanks for the input, let try this, this afternoon and I'll get back to you.
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 35213397
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

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!

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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