Reputation: 51
I have less knowledge in Excel , now I want your help to calculate rate of interest in excel without using RATE command. (so that I can try to convert the formula in php) This is the code we use in Excel
B6= 1
=RATE(B5,B4,-B3,0,B6)*12
It should give a result of "10.75%" as rate.
How can I get the same result without using 'RATE' or any other commands?
Upvotes: 3
Views: 210
Reputation: 96753
From pgc01's answer:
Function MyRATE(nper As Integer, pmt As Double, pv As Double, Optional fv As Double = 0, _
Optional PaymentEnd As Integer = 0, Optional guess As Double = 0.1)
Dim a As Double, b As Double, c As Double ' coefficients of the equation
Dim R As Double, RTmp As Double, i As Integer
' Initialize coefficients and R
R = 1 + guess
a = (pmt * (1 - PaymentEnd) - pv) / (pv + pmt * PaymentEnd)
b = (fv - pmt * PaymentEnd) / (pv + pmt * PaymentEnd)
c = (-pmt * (1 - PaymentEnd) - fv) / (pv + pmt * PaymentEnd)
' Iterate
For i = 1 To 20
RTmp = R - (R ^ (nper + 1) + a * R ^ nper + b * R + c) / ((nper + 1) * R ^ nper + a * nper * R ^ (nper - 1) + b)
If Abs(RTmp - R) < 0.0000001 Then Exit For
R = RTmp
Next i
If i <= 20 Then
MyRATE = RTmp - 1
Else
MyRATE = "N/A" ' Must try another guess
End If
End Function
Note:
This is not my code. I have not tested it over a full range of inputs.
EDIT#1:
This is a User Defined Function. User Defined Functions (UDFs) are very easy to install and use:
If you save the workbook, the UDF will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx
To remove the UDF:
To use the UDF from Excel worksheet cell:
=myrate(B5,B4,-B3,0,B6)*12
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
and for specifics on UDFs, see:
http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx
Macros must be enabled for this to work!
Upvotes: 2