UDF array function update issues

PRyankid

Board Regular
Joined
Aug 23, 2005
Messages
144
I have an array function that I have used to make give me the coefficiencts of the Least Squares polynomial regression. The function works fine to solve them, but when I do things like insert or delete rows or columns, the function breaks down and puts the first value of the array in all of the cells of the array. I try to then use Application.CalculateFull, but that does not work until I highlight one instance of the formula, press F2, then Ctrl-Shift-Enter to update the formula. Then I can use Application.CalculateFull to update the rest. Any advice?

Here is the Code:
Code:
Function PolyReg(xvals As Range, yvals As Range, RegOrder As Integer)
    ReDim xVect(0 To RegOrder * 2)
    ReDim yxVect(0 To RegOrder)
    ReDim LHMatrix(0 To RegOrder, 0 To RegOrder)
    Dim ListLen As Integer
    ListLen = Application.WorksheetFunction.Max(xvals.Rows.Count, xvals.Columns.Count)
    
    For i = 1 To ListLen
        xVect(0) = xVect(0) + 1
        For j = 1 To RegOrder * 2
            xVect(j) = xVect(j) + xvals(i) ^ j
        Next j
        yxVect(0) = yxVect(0) + yvals(i)
        For j = 1 To RegOrder
            yxVect(j) = yxVect(j) + yvals(i) * xvals(i) ^ j
        Next j
    Next i
    
    For i = 0 To RegOrder
        For j = 0 To RegOrder
            LHMatrix(i, j) = xVect(RegOrder - j + i)
        Next j
    Next i
    
    yxVect = Application.WorksheetFunction.Transpose(yxVect)
    LHMatrix = Application.WorksheetFunction.MInverse(LHMatrix)
    yxVect = Application.WorksheetFunction.MMult(LHMatrix, yxVect)
    If Selection.Rows.Count > 1 Then
        PolyReg = yxVect
    Else
        PolyReg = Application.WorksheetFunction.Transpose(yxVect)
    End If
End Function
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I think your issue is using the "selection.rows.count" in a function - doesnt sound like a good idea to me (e.g. its going to be very indeterminant) Find another way!
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,877
Members
449,056
Latest member
ruhulaminappu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top