View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Tbeek[_4_] Tbeek[_4_] is offline
external usenet poster
 
Posts: 1
Default solver setcell= to an argument name rather than a cell in the worksheet

I would like to use solver in VBA and setcell= to an argument name
rather than a cell in the worksheet so I can keep the overhead in VBA.

I am writing a least squares routine that will make a function for
polynomial fits, similar to how trend or forcast work

Here is my code, it does not work in VBA because I am mixing ranges
with variables and such.

' Function created by Travis Beek
Function Poly2(Known_Xs, Known_Ys, New_X, Optional Statistics)

If Known_Xs.Rows.Count < Known_Ys.Rows.Count Then
MsgBox "Known_Xs does not have the same number of rows as Known_Ys."
& Chr(13) _
& "The answer in " & ActiveCell.Address & _
" will not be correct until they match.", vbExclamation, "Warning"
End If

For n = 1 To Known_Xs.Rows.Count
X = Known_Xs.Item(n, 1) 'ref(r,c) is relative to the range, not the
sheet
y = Known_Ys.Item(n, 1)
a = 1 'should the constants be outside the loop?
b = 1
c = 1
Sumsqs = (y - (a * X ^ 2 + b * X + c)) ^ 2 + Sumsqs
Next n

SolverOK SetCell:=Sumsqs, MaxMinVal:=2, ByChange:="a,b,c"
SolverSolve
Poly2 = Sumsqs


' For Each blah In Known_Xs.Cells 'also works, loop repeats at
the "Next" operator
' total = total + blah.Value
' Next

End Function


---
Message posted from http://www.ExcelForum.com/