Solved

# Slope Function for VB 6.0

Posted on 2006-11-30
1,503 Views
Anyone know where I can see code for the Slope funtion written in VB?

I suppose I could write one but why reinvent the wheel.

I actually don't need it for two arrays necessarily.  The Y data will just be ascending dates
so I figure i can just use a count of the data in the X array.  I would think that would be valid?
Y       X
1       5
2       9
3       2
4       6
5       5
6       7

0
Question by:mayfieldjr
• 3
• 2

LVL 69

Accepted Solution

Éric Moreau earned 125 total points
Hi mayfieldjr,

I did that in VB.Net a couple of months ago. You should be able to convert it easily.

Here is my code:
Public Shared Function Slope(ByVal pArrayY() As Double, ByVal pArrayX() As Double) As Double
Dim dblEX As Double
Dim dblEX2 As Double
Dim dblEXY As Double
Dim dblEY As Double
Dim intN As Integer
For intCtr As Integer = 0 To pArrayX.Length - 1
If (pArrayX(intCtr) <> Double.MinValue) AndAlso (pArrayY(intCtr) <> Double.MinValue) Then
dblEX += pArrayX(intCtr)
dblEX2 += (pArrayX(intCtr) * pArrayX(intCtr))
dblEXY += (pArrayX(intCtr) * pArrayY(intCtr))
dblEY += pArrayY(intCtr)
intN += 1
End If
Next

Dim dblA As Double
Dim dblB As Double
Dim dblAvgX As Double
Dim dblAvgY As Double

dblAvgX = dblEX / intN
dblAvgY = dblEY / intN
dblA = dblEXY - ((dblEX * dblEY) / intN)
dblB = dblEX2 - ((dblEX * dblEX) / intN)
Return (dblA / dblB)
End Function

Cheers!
0

Author Comment

I'm not sure what Double.MinValue is????

I believe I have everything else.  Is Double.MinValue mean the min value in the array?

Thanks
0

LVL 69

Expert Comment

it is the lowest value a double can hold. You can replace this with a value you are sure you won't find into your array.
0

Author Comment

I have tested your function but am getting a different answer than Excel gives me.

Example:
Y     X
1     7
2     1
3     4
4     8
5     8
6     4
7     9
8     8
9     1

I get .038   Excel gets .05

I either did not translate to VB properly (probably the case) or made some other error.

What do you get using the above data with your function.

I'll keep trying.

Thanks
0

Author Comment

I see MY error now.  Works very well.

Thank you.  You saved me allot of time.
0

## Featured Post

### Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…