Returns a Double specifying the interest rate per period for an annuity.
Function Rate( _ ByVal NPer As Double, _ ByVal Pmt As Double, _ ByVal PV As Double, _ Optional ByVal FV As Double = 0, _ Optional ByVal Due As DueDate = DueDate.EndOfPeriod, _ Optional ByVal Guess As Double = 0.1 _ ) As Double
See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.
An annuity is a series of fixed cash payments made over a period of time. An annuity can be a loan (such as a home mortgage) or an investment (such as a monthly savings plan).
For all arguments, cash paid out (such as deposits to savings) is represented by negative numbers; cash received (such as dividend checks) is represented by positive numbers.
Rate is calculated by iteration. Starting with the value of Guess, Rate cycles through the calculation until the result is accurate to within 0.00001 percent. If Rate cannot find a result after 20 tries, it fails. If your guess is 10 percent and Rate fails, try a different value for Guess.
This example uses the Rate function to calculate the interest rate of a loan given the total number of payments (TotPmts), the amount of the loan payment (Payment), the present value or principal of the loan (PVal), the future value of the loan (FVal), a number that indicates whether the payment is due at the beginning or end of the payment period (PayType), and an approximation of the expected interest rate (Guess).
Sub TestRate() Dim PVal, Payment, TotPmts, APR As Double Dim PayType As DueDate ' Define percentage format. Dim Fmt As String = "##0.00" Dim Response As MsgBoxResult ' Usually 0 for a loan. Dim FVal As Double = 0 ' Guess of 10 percent. Dim Guess As Double = 0.1 PVal = CDbl(InputBox("How much did you borrow?")) Payment = CDbl(InputBox("What's your monthly payment?")) TotPmts = CDbl(InputBox("How many monthly payments do you have to make?")) Response = MsgBox("Do you make payments at the end of the month?", MsgBoxStyle.YesNo) If Response = MsgBoxResult.No Then PayType = DueDate.BegOfPeriod Else PayType = DueDate.EndOfPeriod End If APR = (Rate(TotPmts, -Payment, PVal, FVal, PayType, Guess) * 12) * 100 MsgBox("Your interest rate is " & Format(CInt(APR), Fmt) & " percent.") End Sub
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)