How to use NPer function in VBA?

The NPer function in VBA is used to calculate the number of periods for an investment based on periodic, constant payments and a constant interest rate. This function is typically used in financial analysis to determine the duration of a loan or investment. Here’s the syntax of the NPer function in VBA:

NPer(Rate, Pmt, Pv, [Fv], [Type])
Rate is the interest rate per period. Pmt is the payment made each period; it cannot change over the life of the annuity. Pv is the present value, or the total amount that a series of future payments is worth now. Fv (optional) is the future value, or a cash balance you want to attain after the last payment is made. If Fv is omitted, it is assumed to be 0. Type (optional) is the number 0 or 1 and indicates when payments are due. 0 means at the end of the period, and 1 means at the beginning of the period. If omitted, it is assumed to be 0. Here’s an example of how to use NPer in VBA to calculate the number of periods for a loan:

Sub CalculateNumberOfPeriods()
    Dim interestRate As Double
    Dim payment As Double
    Dim presentValue As Double
    Dim futureValue As Double
    Dim paymentType As Integer
    Dim periods As Double

    ' Example values
    interestRate = 0.05 / 12 ' 5% annual interest rate, converted to monthly
    payment = -150 ' Monthly payment (negative because it's an outflow)
    presentValue = 5000 ' Present value of the loan
    futureValue = 0 ' We want to pay off the entire loan, so future value is 0
    paymentType = 0 ' Payments are made at the end of each period

    ' Calculate the number of periods
    periods = Application.WorksheetFunction.NPer(interestRate, payment, presentValue, futureValue, paymentType)
    
    ' Print the number of periods to the Immediate window
    Debug.Print "The number of periods is: " & periods
End Sub
This VBA code defines a subroutine that calculates the number of payment periods required to pay off a loan of $5000 with a monthly payment of $150 and an annual interest rate of 5%. It then prints the result to the Immediate window in the VBA editor. Make sure to include the Application.WorksheetFunction part before the NPer function to access the financial functions provided by Excel within VBA. Also, note that the payment (Pmt) is represented as a negative number because it’s an outflow of money.

Unlock Your Potential

Excel

Basic - Advanced

Access

Access Basic - Advanced

Power BI

Power BI Basic - Advanced

Help us grow the project

Leave a Reply

Your email address will not be published. Required fields are marked *