View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
No Name
 
Posts: n/a
Default VBA code to perform summation and product summation

There is a typo below = all of the "xcounts" and "ycounts"
should be "nxcount and xycount - sorry about that.

Also, if it isnt clear range e3 and f3 referred to are
counts of the x and y data.

John


-----Original Message-----
Here's a shot at it. Good Luck!

John


Sub Macro1()
Dim xcount As Integer
Dim ycount As Integer
nxcount = Range("e3").Value
nycount = Range("f3").Value
If nxcount < nycount Then GoTo theend
Dim y(1000) As Variant
Dim x(1000) As Variant
Dim xnum(1000) As Variant
Dim xden(1000) As Variant
Dim P As Variant
Dim Knownx As Variant
'
' Read Data in
' X Data in column B, starting in row 5
' Y Data in column C, starting in row 5
' Known x in range named "Known_x"
Knownx = Range("known_x").Value
For j = 1 To nxcount
x(j) = Cells(j + 4, 2).Value
y(j) = Cells(j + 4, 3).Value
Next j
For j = 1 To nxcount
xnum(j) = 1
xden(j) = 1
For k = 1 To nxcount
If k = j Then k = k + 1
If k nxcount Then GoTo skip
xnum(j) = xnum(j) * (Knownx - x(k))
xden(j) = xden(j) * (x(j) - x(k))
Next k
skip:
Next j
'
P = 0
For j = 1 To nxcount
P = P + y(j) * xnum(j) / xden(j)
Next j
'Output Result to Cell H5
Cells(5, 8).Value = P
GoTo Done
theend:
MsgBox ("There must be the same number of x's as
y's"), , "Hold Up!"
Done:
End Sub

-----Original Message-----
Here is my spreadsheet!

Attachment filename:

exp.xls
Download attachment:

http://www.excelforum.com/attachment.php?postid=546352
---
Message posted from http://www.ExcelForum.com/

.

.