View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
merjet merjet is offline
external usenet poster
 
Posts: 812
Default Quadratic Polynomial fit with LINEST()

Copying the values w/o the NA's to elsewhere and doing the regression
on the copied values seems much easier. You can delete the copied
values afterwards if you want.

Hth,
Merjet


Option Base 1
Option Explicit

Sub LinReg2Setup()
'Take two column vectors, ignore NA values
'and regress Y on X returning coefficients
Dim rngX As Range
Dim rngY As Range
Dim c As Range
Dim rtn As Variant
Dim iEnd As Integer
Dim iCt As Integer
Dim ws As Worksheet

Set ws = Sheets("Sheet1")
iEnd = ws.Range("A2").End(xlDown).Row
Set rngY = ws.Range("A2:A" & iEnd)
For Each c In rngY
If c < "NA" Then
iCt = iCt + 1
ws.Cells(iEnd + 1 + iCt, 1) = ws.Cells(c.Row, 1)
ws.Cells(iEnd + 1 + iCt, 2) = ws.Cells(c.Row, 2)
ws.Cells(iEnd + 1 + iCt, 3) = ws.Cells(c.Row, 3)
End If
Next c
Set rngY = ws.Range("A" & iEnd + 2 & ":A" & iEnd + 1 + iCt)
Set rngX = ws.Range("B" & iEnd + 2 & ":C" & iEnd + 1 + iCt)
rtn = LinReg2(rngY, rngX)
rngX.Clear
rngY.Clear
ws.Range("F2:H2") = rtn

End Sub

Function LinReg2(Y As Range, X As Range)
LinReg2 = Application.LinEst(Y, X, True, False)
End Function