PRB:Rate Function Gives Error If It Can't Calculate AccuratelyLast reviewed: May 16, 1996Article ID: Q138536 |
The information in this article applies to:
SYMPTOMSIf the Rate function is unable to converge to within 0.00001 percent of the correct value after 20 iterations, a trappable error will occur. In Visual Basic Version 3.0, the error is "Illegal Function Call," and is error number 5. In Visual Basic Version 4.0, the error is "Invalid procedure call," and is also error number 5.
WORKAROUNDIf the error occurs, retry the Rate function with a different initial guess value. Sometimes a different guess will lead to numerical convergence within the 20 iterations allowed. You might want to set up an error handler that progressively changes the guess and retries the Rate function. If the Rate function continues to fail for all the guess values tried, then it may be necessary to warn the user that an accurate calculation of the rate based on the values for Total Payment, Number of Payments, and Present Value is not possible. The following code implements an error handler to arrive at a Rate value. Add the following code to the Form_Click event to include the error handler, and a new static variable Newguess, which is adjusted each time an error handler is called:
Private Sub Form_Click Dim Fmt As Variant, FVal As Variant, Guess As Variant, PVal As Variant,_ TotPmts As Variant, Payment As Variant, PayType As Variant, _ APR As Variant Static NewGuess NewGuess = 0.02 Const ENDPERIOD = 0, BEGINPERIOD = 1 ' When payments are made. Const MB_YESNO = 4 ' Define Yes/No buttons. Const ID_NO = 7 ' Define No as a response. On Error GoTo Errhandler Fmt = "##0.00" ' Define percentage format. FVal = 0 ' Usually 0 for a loan. Guess = 0.1 ' Guess of 10 percent. PVal = 81709.07 ' Payment = 720.45 TotPmts = 700 PayType = BEGINPERIOD APR = (Rate(TotPmts, -Payment, PVal, FVal, PayType, Guess) * 12) * 100 MsgBox "Your interest rate is " & Format(CInt(APR), Fmt) & "percent." Exit SubErrhandler:
NewGuess = NewGuess + 0.01 If (NewGuess > 1#) Then MsgBox "Calculation of rate not possible-please change Total payments" Exit Sub End If Guess = NewGuess Resume End Sub STATUSThis behavior is by design.
MORE INFORMATION
Steps to Reproduce Behavior in Visual Basic 4.0
|
Additional reference words: 3.00 4.00 vb4win vb4all
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |