Custom Function

smpatty08

Board Regular
Joined
May 16, 2014
Messages
155
I am trying create custom functions for the individual coefficients of a quadratic equation separately to avoid using the LINEST function and creating an array. I have a sub that will display the information I want correctly in a msgbox, but when I create the function I get #VALUE ! error. Here is what I have so far.
Code:
Function QuadA(y As Range, x As Range) As Double
A = Application.Evaluate("=LINEST(y,(x)^{1,2})")
QuadA = Format(A(1), "0.###")
End Function

Function QuadB(y As Range, x As Range) As Double
B = Application.Evaluate("=LINEST(y,(x)^{1,2})")
QuadB = Format(B(2), "0.###")
End Function

Function QuadC(y As Range, x As Range) As Double
C = Application.Evaluate("=LINEST(y,(x)^{1,2})")
QuadC = Format(C(3), "0.###")
End Function

Sub Quad()
x = Application.Evaluate("=linest(E6:E11,(D6:D11)^{1,2})")
MsgBox "Equation is y=" & Format(x(1), "0.###") & "x2+" & Format(x(2), "0.###") & "x+" & Format(x(3), "0.###")
End Sub

The Sub Quad() works and displays the correct information, but none of the function display a number. I think the problem is that the LINEST function doesn't know what property to use, but even if I put .Address after y and x it stills creates an error. Is there somebody obvious that I am doing incorrectly? Any help is appreciated
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
y and x have no meaning as part of a formula string. You could use:

Code:
Function QuadA(y As Range, x As Range) As Double
A = y.worksheet.Evaluate("=LINEST(" & y.address & ",(" & x.address & ")^{1,2})")
QuadA = Format(A(1), "0.###")
End Function
 
Upvote 0
y and x have no meaning as part of a formula string. You could use:

Code:
Function QuadA(y As Range, x As Range) As Double
A = y.worksheet.Evaluate("=LINEST(" & y.address & ",(" & x.address & ")^{1,2})")
QuadA = Format(A(1), "0.###")
End Function

Wow that was a silly oversight. This works perfectly! Thanks for your help
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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