VB Tutor VB.NET 2022 Tutorial VB2019 Tutorial VB6 Tutorial VB Sample Code About Us
Visual Basic Sample Code

Payback Period Calculator

Calculate how long it will take to pay back a loan


The Payback Period Calculator helps determine how long it will take to repay a loan based on the loan amount, interest rate, and monthly payment. This is calculated using the NPer function in Visual Basic.

The formula uses the following parameters:

  • Rate: Interest Rate per period (monthly rate = annual rate / 12)
  • Pmt: Amount of periodic payment
  • PV: Present value or loan amount
  • FV: Future value (set to 0 when loan is fully paid)
  • Due: Payment timing (0 = end of period, 1 = beginning of period)

Try the calculator below to determine your payback period.

Loan Payback Calculator

Your loan will be paid off in
0 years
0 months

How the Calculation Works

The payback period is calculated using the NPer function, which determines the number of periods for an investment based on periodic, constant payments and a constant interest rate.

Interest Rate Conversion

Annual rate is converted to monthly rate: monthlyRate = annualRate / 100 / 12

NPer Function

NPer(monthlyRate, monthlyPayment, -loanAmount, 0, 0)

Period Conversion

Months to years: years = totalMonths / 12

Tip: The loan amount is negative in the calculation because it represents money borrowed (outflow).

Code Implementation

Below are implementations of the payback period calculation in both VB6 and VB.NET:

VB6 Code

Private Sub Command1_Click()
    Dim payment, Loan, Int_Rate As Double
    Dim Num_months, Num_years As Single
    
    ' Get user input
    payment = Val(Txt_Payment.Text)
    Int_Rate = (Val(Txt_Rate.Text) / 100) / 12 ' Convert to monthly rate
    Loan = Val(Txt_PV.Text)
    
    ' Calculate number of months using NPer function
    Num_months = NPer(Int_Rate, payment, -Loan, 0, 0)
    Num_years = Num_months / 12
    
    ' Display results
    Lbl_Years.Caption = Format(Num_years, "0.0") & " years"
    Lbl_Months.Caption = Format(Num_months, "0") & " months"
    
    ' Visualize the payback period
    Call VisualizePayback(Num_years)
End Sub

VB.NET Code

Private Sub btnCalculate_Click(ByVal sender As System.Object, 
                            ByVal e As System.EventArgs) Handles btnCalculate.Click
    Dim payment, loanAmount, annualRate As Double
    Dim monthlyRate, numMonths, numYears As Double
    
    ' Get user input
    If Not Double.TryParse(txtPayment.Text, payment) Then
        MessageBox.Show("Please enter a valid payment amount")
        Return
    End If
    
    If Not Double.TryParse(txtLoanAmount.Text, loanAmount) Then
        MessageBox.Show("Please enter a valid loan amount")
        Return
    End If
    
    If Not Double.TryParse(txtInterestRate.Text, annualRate) Then
        MessageBox.Show("Please enter a valid interest rate")
        Return
    End If
    
    ' Calculate monthly interest rate
    monthlyRate = (annualRate / 100) / 12
    
    ' Calculate number of months using Financial.NPer
    numMonths = Financial.NPer(monthlyRate, payment, -loanAmount, 0, DueDate.EndOfPeriod)
    numYears = numMonths / 12
    
    ' Display results
    lblResultYears.Text = numYears.ToString("F1") & " years"
    lblResultMonths.Text = numMonths.ToString("F0") & " months"
    
    ' Visualize the payback period
    VisualizePayback(numYears)
End Sub