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
- B5= 60
- B4= 16070
- B3= 750000
It should give a result of “10.75%” as rate.
How can I get the same result without using ‘RATE’ or any other commands?
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
This is not my code. I have not tested it over a full range of inputs.
This is a User Defined Function. User Defined Functions (UDFs) are very easy to install and use:
- ALT-F11 brings up the VBE window
ALT-M opens a fresh module
- paste the stuff in and close the VBE window
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:
- bring up the VBE window as above
- clear the code out
- close the VBE window
To use the UDF from Excel worksheet cell:
To learn more about macros in general, see:
and for specifics on UDFs, see:
Macros must be enabled for this to work!