Code to calculate monthly payment

Posted on 2004-11-09
Last Modified: 2008-03-17
I need to calculate my monthly payment given the loan principal, the percentage, and the terms.  Here are my variables.

      dblPrincipal = CDbl(txtPrincipal.Text)
        dblRate = CDbl(txtRate.Text)
        dblYears = CDbl(txtYears.Text)
Question by:gmahler5th
    LVL 34

    Accepted Solution

    Use this :

    dblMonth = dblYears * 12
    dblRateMonth = dblRate / 12

    dblRate = dblPrincipal / ((1 - (1 + dblRateMonth) ^ -dblMonth) / dblRateMonth)

    That Finance Maths subject finally paid off :-)


    LVL 34

    Expert Comment


    dblPayment  = dblPrincipal / ((1 - (1 + dblRateMonth) ^ -dblMonth) / dblRateMonth)
    LVL 25

    Assisted Solution

    There is a function in .net that will calculate that for you, the PMT function. Like

    Sub TestPMT()
            Dim PVal, APR, FVal, Payment, TotPmts As Double
            Dim PayType As DueDate
            Dim Fmt As String
            Dim Response As MsgBoxResult
            Fmt = "###,###,##0.00"   ' Define money format.
            FVal = 0   ' Usually 0 for a loan.
            PVal = CDbl(InputBox("How much do you want to borrow?"))
            APR = CDbl(InputBox("What is the annual percentage rate of your loan?"))
            If APR > 1 Then APR = APR / 100 ' Ensure proper form.
            TotPmts = CDbl(InputBox("How many monthly payments will you make?"))
            Response = MsgBox("Do you make payments at the end of month?", MsgBoxStyle.YesNo)
            If Response = MsgBoxResult.No Then
                PayType = DueDate.BegOfPeriod
                PayType = DueDate.BegOfPeriod
            End If
            Payment = Pmt(APR / 12, TotPmts, -PVal, FVal, PayType)
            MsgBox("Your payment will be " & Format(Payment, Fmt) & " per month.")
        End Sub

    LVL 34

    Expert Comment

    maybe i did spend 6 months for nothing.... :-)

    Author Comment

    Ronald, Thank you for sharing.  I like the ease and flexibility of this function.  I sort of hacked it up and put it in my own subroutine, and I am coming up with a monthly payment in the 10s of thousands. Can you look to see that I have used this function correctly?

     'declare your variables here
        Dim dblPrincipal, dblMonth, dblRateMonth, dblRate, dblYears, dblAmount As Double
        Dim PVal, APR, FVal, Payment, TotPmts As Double
        Dim Fmt As String

        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

            'assign the values from textboxes to the variables
            PVal = CDbl(txtPrincipal.Text)
            APR = CDbl(txtRate.Text)
            TotPmts = CDbl(txtYears.Text)
            dblMonth = dblYears * 12
            dblRateMonth = dblRate / 12

            'calculate the final amount as principal + interest
            Fmt = "###,###,##0.00"   ' Define money format.
            FVal = 0   ' Usually 0 for a loan.
            Payment = Pmt(APR / 12, TotPmts, -PVal, FVal)

            'put the calculated amount in Payment Amount textbox
            txtAmount.Text = Format(Payment, Fmt)
        End Sub

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now