vb6parse 1.0.0

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="VB6Parse Library Reference - pmt - Financial">
    <title>pmt - Financial - VB6Parse Library Reference</title>
    <link rel="stylesheet" href="../../../assets/css/style.css">
    <link rel="stylesheet" href="../../../assets/css/docs-style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
    <script src="../../../assets/js/theme-switcher.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
    <script>hljs.highlightAll();</script>
</head>
<body>
    <header class="docs-header">
        <div class="container">
            <h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/financial/index.html">Financial</a> / pmt</h1>
            <p class="tagline">VB6 Library Reference</p>
        </div>
    </header>

    <nav class="docs-nav">
        <div class="container">
            <a href="../../../index.html">Home</a>
            <a href="../../../library/index.html">Library Reference</a>
            <a href="../../../documentation.html">Documentation</a>
            <a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
            <a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
            <button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
                <span class="theme-icon">🌙</span>
            </button>
        </div>
    </nav>

    <main class="container">
        
        <article class="library-item">
            <h1 id="pmt-function">Pmt Function</h1>
<p>Returns a Double specifying the payment for an annuity based on periodic, fixed payments and a fixed interest rate.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Pmt(rate, nper, pv, [fv], [type])</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>rate</code> - Required. Double specifying interest rate per period. For example, if you get a car loan at an annual percentage rate (APR) of 10% and make monthly payments, the rate per period is 0.1/12, or 0.0083.</li>
<li><code>nper</code> - Required. Integer specifying total number of payment periods in the annuity. For example, if you make monthly payments on a 4-year car loan, your loan has 4 * 12 (or 48) payment periods.</li>
<li><code>pv</code> - Required. Double specifying present value (or lump sum) that a series of payments to be paid in the future is worth now. For example, when you borrow money to buy a car, the loan amount is the present value to the lender of the monthly car payments you will make.</li>
<li><code>fv</code> - Optional. Variant specifying future value or cash balance you want after you've made the final payment. For example, the future value of a loan is $0 because that's its value after the final payment. However, if you want to save $50,000 over 18 years for your child's education, then $50,000 is the future value. If omitted, 0 is assumed.</li>
<li><code>type</code> - Optional. Variant specifying when payments are due. Use 0 if payments are due at the end of the payment period, or use 1 if payments are due at the beginning of the period. If omitted, 0 is assumed.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>Double</code> specifying the payment amount per period. The payment includes principal and interest but includes no taxes, reserve payments, or fees sometimes associated with loans.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>Pmt</code> function is one of the most commonly used financial functions in VB6. It calculates the periodic payment
required to pay off a loan or to accumulate a certain amount in a savings plan, given a fixed interest rate and
fixed payment periods.
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).
The <code>rate</code> and <code>nper</code> arguments must be calculated using payment periods expressed in the same units. For example, if <code>rate</code>
is calculated using months, <code>nper</code> must also be calculated using months.
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.
<strong>Important</strong>: The payment returned by <code>Pmt</code> is typically a negative number when calculating loan payments, because
it represents money flowing out. When calculating savings deposits, the payment is positive if you want to know
how much to deposit (money flowing out).</p>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Mortgage Calculation</strong>: Calculate monthly mortgage payments for home loans</li>
<li><strong>Auto Loan Payments</strong>: Determine monthly car loan payments</li>
<li><strong>Savings Goals</strong>: Calculate required monthly deposits to reach a savings target</li>
<li><strong>Retirement Planning</strong>: Determine periodic contributions needed for retirement funds</li>
<li><strong>Lease Payments</strong>: Calculate lease payment amounts for equipment or vehicles</li>
<li><strong>Student Loan Amortization</strong>: Compute monthly student loan payments</li>
<li><strong>Investment Planning</strong>: Determine periodic investment amounts to reach financial goals</li>
<li><strong>Loan Comparison</strong>: Compare different loan options by payment amount</li>
</ol>
<h2 id="basic-examples">Basic Examples</h2>
<h3 id="example-1-simple-loan-payment">Example 1: Simple Loan Payment</h3>
<pre><code class="language-vbnet">&#x27; Calculate monthly payment for a $20,000 loan at 6% APR for 5 years
Dim monthlyPayment As Double
monthlyPayment = Pmt(0.06 / 12, 5 * 12, 20000)
&#x27; Returns approximately -386.66 (negative because it&#x27;s money paid out)</code></pre>
<h3 id="example-2-mortgage-payment">Example 2: Mortgage Payment</h3>
<pre><code class="language-vbnet">&#x27; $200,000 home loan, 30 years, 4.5% APR
Dim mortgagePayment As Double
mortgagePayment = Pmt(0.045 / 12, 30 * 12, 200000)
&#x27; Returns approximately -1,013.37 per month</code></pre>
<h3 id="example-3-savings-plan">Example 3: Savings Plan</h3>
<pre><code class="language-vbnet">&#x27; How much to save monthly to accumulate $50,000 in 10 years at 5% annual return?
Dim monthlyDeposit As Double
monthlyDeposit = Pmt(0.05 / 12, 10 * 12, 0, -50000)
&#x27; Returns approximately -322.67 per month (negative = deposit)</code></pre>
<h3 id="example-4-payment-due-at-beginning-of-period">Example 4: Payment Due at Beginning of Period</h3>
<pre><code class="language-vbnet">&#x27; Lease payment due at start of month
Dim leasePayment As Double
leasePayment = Pmt(0.08 / 12, 36, 25000, 0, 1)
&#x27; Returns slightly lower payment due to beginning-of-period timing</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="pattern-1-calculateloanpayment">Pattern 1: <code>CalculateLoanPayment</code></h3>
<pre><code class="language-vbnet">Function CalculateLoanPayment(loanAmount As Double, _
                              annualRate As Double, _
                              years As Integer) As Double
    Dim monthlyRate As Double
    Dim numPayments As Integer
    monthlyRate = annualRate / 12
    numPayments = years * 12
    &#x27; Return positive value (absolute value of payment)
    CalculateLoanPayment = Abs(Pmt(monthlyRate, numPayments, loanAmount))
End Function</code></pre>
<h3 id="pattern-2-calculatetotalinterest">Pattern 2: <code>CalculateTotalInterest</code></h3>
<pre><code class="language-vbnet">Function CalculateTotalInterest(principal As Double, _
                                annualRate As Double, _
                                years As Integer) As Double
    Dim payment As Double
    Dim totalPaid As Double
    Dim numPayments As Integer
    numPayments = years * 12
    payment = Abs(Pmt(annualRate / 12, numPayments, principal))
    totalPaid = payment * numPayments
    CalculateTotalInterest = totalPaid - principal
End Function</code></pre>
<h3 id="pattern-3-compareloanoptions">Pattern 3: <code>CompareLoanOptions</code></h3>
<pre><code class="language-vbnet">Sub CompareLoanOptions(amount As Double)
    Dim payment15 As Double
    Dim payment30 As Double
    Dim totalInterest15 As Double
    Dim totalInterest30 As Double
    &#x27; 15-year loan at 3.5%
    payment15 = Abs(Pmt(0.035 / 12, 15 * 12, amount))
    totalInterest15 = (payment15 * 15 * 12) - amount
    &#x27; 30-year loan at 4.0%
    payment30 = Abs(Pmt(0.04 / 12, 30 * 12, amount))
    totalInterest30 = (payment30 * 30 * 12) - amount
    Debug.Print &quot;15-year: $&quot; &amp; Format(payment15, &quot;0.00&quot;) &amp; &quot;/mo, Total Interest: $&quot; &amp; Format(totalInterest15, &quot;#,##0&quot;)
    Debug.Print &quot;30-year: $&quot; &amp; Format(payment30, &quot;0.00&quot;) &amp; &quot;/mo, Total Interest: $&quot; &amp; Format(totalInterest30, &quot;#,##0&quot;)
End Sub</code></pre>
<h3 id="pattern-4-calculateaffordableamount">Pattern 4: <code>CalculateAffordableAmount</code></h3>
<pre><code class="language-vbnet">Function CalculateAffordableAmount(monthlyPayment As Double, _
                                   annualRate As Double, _
                                   years As Integer) As Double
    &#x27; Calculate how much you can borrow given a payment amount
    Dim monthlyRate As Double
    Dim numPayments As Integer
    Dim principal As Double
    monthlyRate = annualRate / 12
    numPayments = years * 12
    &#x27; Use negative payment because Pmt returns negative for outflows
    principal = PV(monthlyRate, numPayments, -monthlyPayment)
    CalculateAffordableAmount = principal
End Function</code></pre>
<h3 id="pattern-5-savingscalculator">Pattern 5: <code>SavingsCalculator</code></h3>
<pre><code class="language-vbnet">Function CalculateSavingsDeposit(targetAmount As Double, _
                                 years As Integer, _
                                 annualReturn As Double) As Double
    Dim monthlyRate As Double
    Dim numPayments As Integer
    monthlyRate = annualReturn / 12
    numPayments = years * 12
    &#x27; Use negative FV because it&#x27;s a goal (money we want)
    &#x27; Return positive value (absolute value)
    CalculateSavingsDeposit = Abs(Pmt(monthlyRate, numPayments, 0, -targetAmount))
End Function</code></pre>
<h3 id="pattern-6-validatepmtparameters">Pattern 6: <code>ValidatePmtParameters</code></h3>
<pre><code class="language-vbnet">Function ValidatePmtParameters(rate As Double, nper As Integer, _
                               pv As Double) As Boolean
    ValidatePmtParameters = False
    If nper &lt;= 0 Then
        MsgBox &quot;Number of periods must be positive&quot;
        Exit Function
    End If
    If rate &lt; 0 Then
        MsgBox &quot;Interest rate cannot be negative&quot;
        Exit Function
    End If
    If pv = 0 Then
        MsgBox &quot;Present value cannot be zero for loan calculations&quot;
        Exit Function
    End If
    ValidatePmtParameters = True
End Function</code></pre>
<h3 id="pattern-7-calculatewithextrapayment">Pattern 7: <code>CalculateWithExtraPayment</code></h3>
<pre><code class="language-vbnet">Function CalculatePayoffWithExtra(principal As Double, _
                                  annualRate As Double, _
                                  years As Integer, _
                                  extraPayment As Double, _
                                  ByRef periodsToPayoff As Integer) As Double
    Dim regularPayment As Double
    Dim totalPayment As Double
    Dim balance As Double
    Dim monthlyRate As Double
    Dim period As Integer
    monthlyRate = annualRate / 12
    regularPayment = Abs(Pmt(monthlyRate, years * 12, principal))
    totalPayment = regularPayment + extraPayment
    balance = principal
    period = 0
    Do While balance &gt; 0 And period &lt; years * 12
        balance = balance * (1 + monthlyRate) - totalPayment
        period = period + 1
    Loop
    periodsToPayoff = period
    CalculatePayoffWithExtra = totalPayment
End Function</code></pre>
<h3 id="pattern-8-amortizationschedule">Pattern 8: <code>AmortizationSchedule</code></h3>
<pre><code class="language-vbnet">Sub CreateAmortizationSchedule(principal As Double, _
                               annualRate As Double, _
                               years As Integer)
    Dim payment As Double
    Dim monthlyRate As Double
    Dim numPayments As Integer
    Dim balance As Double
    Dim interestPaid As Double
    Dim principalPaid As Double
    Dim period As Integer
    monthlyRate = annualRate / 12
    numPayments = years * 12
    payment = Abs(Pmt(monthlyRate, numPayments, principal))
    balance = principal
    Debug.Print &quot;Period&quot;, &quot;Payment&quot;, &quot;Interest&quot;, &quot;Principal&quot;, &quot;Balance&quot;
    Debug.Print String(60, &quot;-&quot;)
    For period = 1 To numPayments
        interestPaid = balance * monthlyRate
        principalPaid = payment - interestPaid
        balance = balance - principalPaid
        Debug.Print period, _
                    Format(payment, &quot;0.00&quot;), _
                    Format(interestPaid, &quot;0.00&quot;), _
                    Format(principalPaid, &quot;0.00&quot;), _
                    Format(balance, &quot;0.00&quot;)
    Next period
End Sub</code></pre>
<h3 id="pattern-9-biweeklypaymentcalculator">Pattern 9: <code>BiweeklyPaymentCalculator</code></h3>
<pre><code class="language-vbnet">Function CalculateBiweeklyPayment(principal As Double, _
                                  annualRate As Double, _
                                  years As Integer) As Double
    Dim monthlyPayment As Double
    Dim biweeklyPayment As Double
    &#x27; Calculate monthly payment
    monthlyPayment = Abs(Pmt(annualRate / 12, years * 12, principal))
    &#x27; Biweekly is half the monthly payment
    &#x27; This results in 26 payments per year instead of 24, paying off faster
    biweeklyPayment = monthlyPayment / 2
    CalculateBiweeklyPayment = biweeklyPayment
End Function</code></pre>
<h3 id="pattern-10-refinanceanalysis">Pattern 10: <code>RefinanceAnalysis</code></h3>
<pre><code class="language-vbnet">Function ShouldRefinance(currentBalance As Double, _
                         currentRate As Double, _
                         currentYearsLeft As Integer, _
                         newRate As Double, _
                         newYears As Integer, _
                         closingCosts As Double) As Boolean
    Dim currentPayment As Double
    Dim newPayment As Double
    Dim monthlySavings As Double
    Dim breakEvenMonths As Double
    currentPayment = Abs(Pmt(currentRate / 12, currentYearsLeft * 12, currentBalance))
    newPayment = Abs(Pmt(newRate / 12, newYears * 12, currentBalance))
    monthlySavings = currentPayment - newPayment
    If monthlySavings &lt;= 0 Then
        ShouldRefinance = False
        Exit Function
    End If
    breakEvenMonths = closingCosts / monthlySavings
    &#x27; Refinance if break-even is less than 3 years and you&#x27;ll stay that long
    ShouldRefinance = (breakEvenMonths &lt;= 36)
End Function</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="example-1-comprehensive-loan-calculator-class">Example 1: Comprehensive Loan Calculator Class</h3>
<pre><code class="language-vbnet">&#x27; Complete loan calculator with full amortization
Class LoanCalculator
    Private m_principal As Double
    Private m_annualRate As Double
    Private m_years As Integer
    Private m_paymentType As Integer  &#x27; 0 = end of period, 1 = beginning
    Public Property Let Principal(value As Double)
        If value &gt; 0 Then m_principal = value
    End Property
    Public Property Let AnnualRate(value As Double)
        If value &gt;= 0 Then m_annualRate = value
    End Property
    Public Property Let Years(value As Integer)
        If value &gt; 0 Then m_years = value
    End Property
    Public Property Let PaymentType(value As Integer)
        If value = 0 Or value = 1 Then m_paymentType = value
    End Property
    Public Function GetMonthlyPayment() As Double
        Dim monthlyRate As Double
        Dim numPayments As Integer
        monthlyRate = m_annualRate / 12
        numPayments = m_years * 12
        GetMonthlyPayment = Abs(Pmt(monthlyRate, numPayments, m_principal, 0, m_paymentType))
    End Function
    Public Function GetTotalInterest() As Double
        Dim payment As Double
        Dim totalPaid As Double
        payment = GetMonthlyPayment()
        totalPaid = payment * m_years * 12
        GetTotalInterest = totalPaid - m_principal
    End Function
    Public Function GetTotalPaid() As Double
        GetTotalPaid = GetMonthlyPayment() * m_years * 12
    End Function
    Public Function GetAmortizationSchedule() As Collection
        Dim schedule As Collection
        Dim payment As Double
        Dim monthlyRate As Double
        Dim balance As Double
        Dim interestPaid As Double
        Dim principalPaid As Double
        Dim period As Integer
        Dim entry As Object
        Set schedule = New Collection
        monthlyRate = m_annualRate / 12
        payment = GetMonthlyPayment()
        balance = m_principal
        For period = 1 To m_years * 12
            interestPaid = balance * monthlyRate
            principalPaid = payment - interestPaid
            balance = balance - principalPaid
            Set entry = CreateObject(&quot;Scripting.Dictionary&quot;)
            entry.Add &quot;Period&quot;, period
            entry.Add &quot;Payment&quot;, payment
            entry.Add &quot;Interest&quot;, interestPaid
            entry.Add &quot;Principal&quot;, principalPaid
            entry.Add &quot;Balance&quot;, balance
            schedule.Add entry
        Next period
        Set GetAmortizationSchedule = schedule
    End Function
    Public Function GenerateReport() As String
        Dim report As String
        report = &quot;Loan Analysis Report&quot; &amp; vbCrLf
        report = report &amp; String(50, &quot;=&quot;) &amp; vbCrLf
        report = report &amp; &quot;Loan Amount: $&quot; &amp; Format(m_principal, &quot;#,##0.00&quot;) &amp; vbCrLf
        report = report &amp; &quot;Annual Rate: &quot; &amp; Format(m_annualRate * 100, &quot;0.00&quot;) &amp; &quot;%&quot; &amp; vbCrLf
        report = report &amp; &quot;Term: &quot; &amp; m_years &amp; &quot; years&quot; &amp; vbCrLf
        report = report &amp; &quot;Payment Type: &quot; &amp; IIf(m_paymentType = 0, &quot;End of Period&quot;, &quot;Beginning of Period&quot;) &amp; vbCrLf
        report = report &amp; String(50, &quot;-&quot;) &amp; vbCrLf
        report = report &amp; &quot;Monthly Payment: $&quot; &amp; Format(GetMonthlyPayment(), &quot;#,##0.00&quot;) &amp; vbCrLf
        report = report &amp; &quot;Total Paid: $&quot; &amp; Format(GetTotalPaid(), &quot;#,##0.00&quot;) &amp; vbCrLf
        report = report &amp; &quot;Total Interest: $&quot; &amp; Format(GetTotalInterest(), &quot;#,##0.00&quot;) &amp; vbCrLf
        report = report &amp; &quot;Interest as % of Principal: &quot; &amp; _
                 Format((GetTotalInterest() / m_principal) * 100, &quot;0.0&quot;) &amp; &quot;%&quot;
        GenerateReport = report
    End Function
    Public Function CalculatePayoffDate(startDate As Date) As Date
        CalculatePayoffDate = DateAdd(&quot;m&quot;, m_years * 12, startDate)
    End Function
End Class</code></pre>
<h3 id="example-2-retirement-savings-planner">Example 2: Retirement Savings Planner</h3>
<pre><code class="language-vbnet">&#x27; Plan retirement savings with multiple scenarios
Class RetirementPlanner
    Private m_currentAge As Integer
    Private m_retirementAge As Integer
    Private m_targetAmount As Double
    Private m_expectedReturn As Double
    Private m_currentSavings As Double
    Public Sub Initialize(currentAge As Integer, retirementAge As Integer, _
                         targetAmount As Double, expectedReturn As Double, _
                         currentSavings As Double)
        m_currentAge = currentAge
        m_retirementAge = retirementAge
        m_targetAmount = targetAmount
        m_expectedReturn = expectedReturn
        m_currentSavings = currentSavings
    End Sub
    Public Function GetRequiredMonthlyContribution() As Double
        Dim yearsToRetirement As Integer
        Dim monthlyRate As Double
        Dim numPayments As Integer
        Dim futureValueNeeded As Double
        yearsToRetirement = m_retirementAge - m_currentAge
        monthlyRate = m_expectedReturn / 12
        numPayments = yearsToRetirement * 12
        &#x27; Account for current savings growing
        futureValueNeeded = m_targetAmount - (m_currentSavings * ((1 + monthlyRate) ^ numPayments))
        &#x27; Calculate required payment
        GetRequiredMonthlyContribution = Abs(Pmt(monthlyRate, numPayments, 0, -futureValueNeeded))
    End Function
    Public Function ProjectSavingsGrowth() As Collection
        Dim projections As Collection
        Dim monthlyContribution As Double
        Dim balance As Double
        Dim monthlyRate As Double
        Dim year As Integer
        Dim month As Integer
        Dim entry As Object
        Set projections = New Collection
        monthlyContribution = GetRequiredMonthlyContribution()
        monthlyRate = m_expectedReturn / 12
        balance = m_currentSavings
        For year = m_currentAge To m_retirementAge - 1
            For month = 1 To 12
                balance = balance * (1 + monthlyRate) + monthlyContribution
            Next month
            Set entry = CreateObject(&quot;Scripting.Dictionary&quot;)
            entry.Add &quot;Age&quot;, year + 1
            entry.Add &quot;Balance&quot;, balance
            entry.Add &quot;YearlyContribution&quot;, monthlyContribution * 12
            projections.Add entry
        Next year
        Set ProjectSavingsGrowth = projections
    End Function
    Public Function AnalyzeReturnScenarios() As String
        Dim report As String
        Dim rates() As Double
        Dim i As Integer
        Dim payment As Double
        ReDim rates(0 To 4)
        rates(0) = 0.04
        rates(1) = 0.06
        rates(2) = 0.08
        rates(3) = 0.10
        rates(4) = 0.12
        report = &quot;Retirement Contribution Analysis&quot; &amp; vbCrLf
        report = report &amp; String(50, &quot;=&quot;) &amp; vbCrLf
        report = report &amp; &quot;Target Amount: $&quot; &amp; Format(m_targetAmount, &quot;#,##0&quot;) &amp; vbCrLf
        report = report &amp; &quot;Years to Retirement: &quot; &amp; (m_retirementAge - m_currentAge) &amp; vbCrLf
        report = report &amp; String(50, &quot;-&quot;) &amp; vbCrLf
        report = report &amp; &quot;Return Rate    Monthly Contribution&quot; &amp; vbCrLf
        report = report &amp; String(50, &quot;-&quot;) &amp; vbCrLf
        For i = 0 To 4
            m_expectedReturn = rates(i)
            payment = GetRequiredMonthlyContribution()
            report = report &amp; Format(rates(i) * 100, &quot;00.0&quot;) &amp; &quot;%          $&quot; &amp; _
                     Format(payment, &quot;#,##0.00&quot;) &amp; vbCrLf
        Next i
        AnalyzeReturnScenarios = report
    End Function
    Public Function GenerateRetirementPlan() As String
        Dim plan As String
        Dim monthlyContribution As Double
        Dim totalContributions As Double
        Dim yearsToRetirement As Integer
        monthlyContribution = GetRequiredMonthlyContribution()
        yearsToRetirement = m_retirementAge - m_currentAge
        totalContributions = monthlyContribution * 12 * yearsToRetirement
        plan = &quot;Personalized Retirement Plan&quot; &amp; vbCrLf
        plan = plan &amp; String(50, &quot;=&quot;) &amp; vbCrLf
        plan = plan &amp; &quot;Current Age: &quot; &amp; m_currentAge &amp; vbCrLf
        plan = plan &amp; &quot;Retirement Age: &quot; &amp; m_retirementAge &amp; vbCrLf
        plan = plan &amp; &quot;Current Savings: $&quot; &amp; Format(m_currentSavings, &quot;#,##0&quot;) &amp; vbCrLf
        plan = plan &amp; &quot;Target Amount: $&quot; &amp; Format(m_targetAmount, &quot;#,##0&quot;) &amp; vbCrLf
        plan = plan &amp; &quot;Expected Return: &quot; &amp; Format(m_expectedReturn * 100, &quot;0.0&quot;) &amp; &quot;%&quot; &amp; vbCrLf
        plan = plan &amp; String(50, &quot;-&quot;) &amp; vbCrLf
        plan = plan &amp; &quot;Required Monthly Contribution: $&quot; &amp; Format(monthlyContribution, &quot;#,##0.00&quot;) &amp; vbCrLf
        plan = plan &amp; &quot;Total You&#x27;ll Contribute: $&quot; &amp; Format(totalContributions, &quot;#,##0&quot;) &amp; vbCrLf
        plan = plan &amp; &quot;Expected Investment Growth: $&quot; &amp; _
               Format(m_targetAmount - totalContributions - m_currentSavings, &quot;#,##0&quot;)
        GenerateRetirementPlan = plan
    End Function
End Class</code></pre>
<h3 id="example-3-auto-loan-comparison-tool">Example 3: Auto Loan Comparison Tool</h3>
<pre><code class="language-vbnet">&#x27; Compare multiple auto loan options
Module AutoLoanAnalyzer
    Private Type LoanOption
        LenderName As String
        Rate As Double
        Term As Integer
        DownPayment As Double
        TradeInValue As Double
        Fees As Double
    End Type
    Public Function AnalyzeLoanOptions(carPrice As Double, _
                                      options() As LoanOption) As String
        Dim report As String
        Dim i As Integer
        Dim loanAmount As Double
        Dim payment As Double
        Dim totalCost As Double
        Dim totalInterest As Double
        Dim bestOption As Integer
        Dim lowestCost As Double
        report = &quot;Auto Loan Comparison&quot; &amp; vbCrLf
        report = report &amp; &quot;Vehicle Price: $&quot; &amp; Format(carPrice, &quot;#,##0&quot;) &amp; vbCrLf
        report = report &amp; String(70, &quot;=&quot;) &amp; vbCrLf
        report = report &amp; &quot;Lender          Rate   Term   Payment   Total Cost   Interest&quot; &amp; vbCrLf
        report = report &amp; String(70, &quot;-&quot;) &amp; vbCrLf
        lowestCost = 999999999
        For i = LBound(options) To UBound(options)
            loanAmount = carPrice - options(i).DownPayment - options(i).TradeInValue + options(i).Fees
            payment = Abs(Pmt(options(i).Rate / 12, options(i).Term, loanAmount))
            totalCost = payment * options(i).Term + options(i).DownPayment - options(i).TradeInValue
            totalInterest = (payment * options(i).Term) - loanAmount
            report = report &amp; Left(options(i).LenderName &amp; Space(15), 15) &amp; &quot; &quot;
            report = report &amp; Format(options(i).Rate * 100, &quot;0.0&quot;) &amp; &quot;%  &quot;
            report = report &amp; Format(options(i).Term, &quot;00&quot;) &amp; &quot;mo  &quot;
            report = report &amp; &quot;$&quot; &amp; Format(payment, &quot;000.00&quot;) &amp; &quot;  &quot;
            report = report &amp; &quot;$&quot; &amp; Format(totalCost, &quot;#,##0&quot;) &amp; &quot;  &quot;
            report = report &amp; &quot;$&quot; &amp; Format(totalInterest, &quot;#,##0&quot;)
            If totalCost &lt; lowestCost Then
                lowestCost = totalCost
                bestOption = i
                report = report &amp; &quot; *BEST*&quot;
            End If
            report = report &amp; vbCrLf
        Next i
        report = report &amp; String(70, &quot;-&quot;) &amp; vbCrLf
        report = report &amp; &quot;Recommended: &quot; &amp; options(bestOption).LenderName
        AnalyzeLoanOptions = report
    End Function
    Public Function CalculateBreakEvenTerm(price As Double, rate1 As Double, _
                                          term1 As Integer, rate2 As Double, _
                                          term2 As Integer) As String
        Dim payment1 As Double
        Dim payment2 As Double
        Dim totalCost1 As Double
        Dim totalCost2 As Double
        Dim monthlySavings As Double
        payment1 = Abs(Pmt(rate1 / 12, term1, price))
        payment2 = Abs(Pmt(rate2 / 12, term2, price))
        totalCost1 = payment1 * term1
        totalCost2 = payment2 * term2
        monthlySavings = payment1 - payment2
        CalculateBreakEvenTerm = &quot;Option 1: &quot; &amp; term1 &amp; &quot; months at &quot; &amp; _
                                Format(rate1 * 100, &quot;0.0&quot;) &amp; &quot;% = $&quot; &amp; _
                                Format(payment1, &quot;0.00&quot;) &amp; &quot;/mo (Total: $&quot; &amp; _
                                Format(totalCost1, &quot;#,##0&quot;) &amp; &quot;)&quot; &amp; vbCrLf &amp; _
                                &quot;Option 2: &quot; &amp; term2 &amp; &quot; months at &quot; &amp; _
                                Format(rate2 * 100, &quot;0.0&quot;) &amp; &quot;% = $&quot; &amp; _
                                Format(payment2, &quot;0.00&quot;) &amp; &quot;/mo (Total: $&quot; &amp; _
                                Format(totalCost2, &quot;#,##0&quot;) &amp; &quot;)&quot;
    End Function
End Module</code></pre>
<h3 id="example-4-mortgage-affordability-calculator">Example 4: Mortgage Affordability Calculator</h3>
<pre><code class="language-vbnet">&#x27; Calculate home affordability based on income
Class MortgageAffordabilityCalculator
    Private m_monthlyIncome As Double
    Private m_monthlyDebts As Double
    Private m_downPaymentPercent As Double
    Private m_annualRate As Double
    Private m_loanTermYears As Integer
    Private m_propertyTaxRate As Double
    Private m_insuranceRate As Double
    Public Sub SetIncome(monthlyIncome As Double)
        m_monthlyIncome = monthlyIncome
    End Sub
    Public Sub SetDebts(monthlyDebts As Double)
        m_monthlyDebts = monthlyDebts
    End Sub
    Public Sub SetLoanTerms(downPaymentPercent As Double, _
                           annualRate As Double, years As Integer)
        m_downPaymentPercent = downPaymentPercent
        m_annualRate = annualRate
        m_loanTermYears = years
    End Sub
    Public Sub SetHousingCosts(propertyTaxRate As Double, insuranceRate As Double)
        m_propertyTaxRate = propertyTaxRate
        m_insuranceRate = insuranceRate
    End Sub
    Public Function GetMaxMonthlyPayment() As Double
        &#x27; Use 28% front-end ratio (housing costs / gross income)
        &#x27; and 36% back-end ratio (total debt / gross income)
        Dim maxByFrontEnd As Double
        Dim maxByBackEnd As Double
        maxByFrontEnd = m_monthlyIncome * 0.28
        maxByBackEnd = (m_monthlyIncome * 0.36) - m_monthlyDebts
        &#x27; Use the more conservative (lower) value
        If maxByFrontEnd &lt; maxByBackEnd Then
            GetMaxMonthlyPayment = maxByFrontEnd
        Else
            GetMaxMonthlyPayment = maxByBackEnd
        End If
    End Function
    Public Function GetAffordableHomePrice() As Double
        Dim maxPayment As Double
        Dim principalAndInterest As Double
        Dim loanAmount As Double
        Dim homePrice As Double
        maxPayment = GetMaxMonthlyPayment()
        &#x27; Estimate property tax and insurance (rough approximation)
        &#x27; Actual payment = P&amp;I + taxes + insurance
        &#x27; taxes ≈ (price × tax rate) / 12
        &#x27; insurance ≈ (price × insurance rate) / 12
        &#x27; P&amp;I = maxPayment - (price × (taxRate + insuranceRate) / 12)
        &#x27; For simplicity, allocate 80% of max payment to P&amp;I
        principalAndInterest = maxPayment * 0.8
        &#x27; Use PV to find loan amount from payment
        loanAmount = Abs(PV(m_annualRate / 12, m_loanTermYears * 12, -principalAndInterest))
        &#x27; Calculate home price from loan amount
        homePrice = loanAmount / (1 - m_downPaymentPercent)
        GetAffordableHomePrice = homePrice
    End Function
    Public Function GenerateAffordabilityReport() As String
        Dim report As String
        Dim maxPayment As Double
        Dim affordablePrice As Double
        Dim downPayment As Double
        Dim loanAmount As Double
        Dim estimatedPayment As Double
        maxPayment = GetMaxMonthlyPayment()
        affordablePrice = GetAffordableHomePrice()
        downPayment = affordablePrice * m_downPaymentPercent
        loanAmount = affordablePrice - downPayment
        estimatedPayment = Abs(Pmt(m_annualRate / 12, m_loanTermYears * 12, loanAmount))
        report = &quot;Mortgage Affordability Analysis&quot; &amp; vbCrLf
        report = report &amp; String(50, &quot;=&quot;) &amp; vbCrLf
        report = report &amp; &quot;Income &amp; Debt Information:&quot; &amp; vbCrLf
        report = report &amp; &quot;  Monthly Gross Income: $&quot; &amp; Format(m_monthlyIncome, &quot;#,##0&quot;) &amp; vbCrLf
        report = report &amp; &quot;  Monthly Debt Payments: $&quot; &amp; Format(m_monthlyDebts, &quot;#,##0&quot;) &amp; vbCrLf
        report = report &amp; String(50, &quot;-&quot;) &amp; vbCrLf
        report = report &amp; &quot;Loan Parameters:&quot; &amp; vbCrLf
        report = report &amp; &quot;  Down Payment: &quot; &amp; Format(m_downPaymentPercent * 100, &quot;0&quot;) &amp; &quot;%&quot; &amp; vbCrLf
        report = report &amp; &quot;  Interest Rate: &quot; &amp; Format(m_annualRate * 100, &quot;0.00&quot;) &amp; &quot;%&quot; &amp; vbCrLf
        report = report &amp; &quot;  Loan Term: &quot; &amp; m_loanTermYears &amp; &quot; years&quot; &amp; vbCrLf
        report = report &amp; String(50, &quot;-&quot;) &amp; vbCrLf
        report = report &amp; &quot;Affordability Results:&quot; &amp; vbCrLf
        report = report &amp; &quot;  Max Monthly Payment: $&quot; &amp; Format(maxPayment, &quot;#,##0&quot;) &amp; vbCrLf
        report = report &amp; &quot;  Affordable Home Price: $&quot; &amp; Format(affordablePrice, &quot;#,##0&quot;) &amp; vbCrLf
        report = report &amp; &quot;  Required Down Payment: $&quot; &amp; Format(downPayment, &quot;#,##0&quot;) &amp; vbCrLf
        report = report &amp; &quot;  Loan Amount: $&quot; &amp; Format(loanAmount, &quot;#,##0&quot;) &amp; vbCrLf
        report = report &amp; &quot;  Est. Monthly P&amp;I: $&quot; &amp; Format(estimatedPayment, &quot;#,##0&quot;)
        GenerateAffordabilityReport = report
    End Function
End Class</code></pre>
<h2 id="error-handling">Error Handling</h2>
<p>The <code>Pmt</code> function can raise errors in the following situations:
- <strong>Invalid Procedure Call (Error 5)</strong>: When:
  - <code>nper</code> is 0 or negative
  - <code>rate</code> is -1 (causes division by zero in the formula)
- <strong>Type Mismatch (Error 13)</strong>: When arguments cannot be converted to numeric values
- <strong>Overflow (Error 6)</strong>: When calculated payment exceeds Double range
Always validate input parameters:</p>
<pre><code class="language-vbnet">On Error Resume Next
payment = Pmt(rate, nper, pv, fv, type)
If Err.Number &lt;&gt; 0 Then
    MsgBox &quot;Error calculating payment: &quot; &amp; Err.Description
    Err.Clear
End If
On Error GoTo 0</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li>The <code>Pmt</code> function is very fast for individual calculations</li>
<li>For amortization schedules with hundreds of periods, pre-calculate the payment once</li>
<li>Avoid calling <code>Pmt</code> repeatedly in tight loops with the same parameters</li>
<li>Use variables to store intermediate calculations (monthly rate, etc.)</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Convert Rates Properly</strong>: Always divide annual rates by 12 for monthly payments</li>
<li><strong>Match Time Units</strong>: Ensure rate and nper use the same time period</li>
<li><strong>Use Absolute Value</strong>: Use <code>Abs()</code> to display positive payment amounts to users</li>
<li><strong>Validate Inputs</strong>: Check that nper &gt; 0 and rate is reasonable before calling</li>
<li><strong>Handle Sign Conventions</strong>: Remember negative = outflow, positive = inflow</li>
<li><strong>Round for Display</strong>: Use <code>Format()</code> to display payments with 2 decimal places</li>
<li><strong>Consider Type Parameter</strong>: Use type=1 for beginning-of-period payments (leases, etc.)</li>
<li><strong>Document Assumptions</strong>: Clearly state what rate, term, and conditions are used</li>
<li><strong>Test Edge Cases</strong>: Verify behavior with 0% rate, very short/long terms</li>
<li><strong>Combine with Other Functions</strong>: Use with <code>IPmt</code>, <code>PPmt</code>, <code>PV</code>, <code>FV</code> for complete analysis</li>
</ol>
<h2 id="comparison-with-related-functions">Comparison with Related Functions</h2>
<table>
<thead>
<tr>
<th>Function</th>
<th>Purpose</th>
<th>Returns</th>
<th>Use Case</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Pmt</strong></td>
<td>Calculate periodic payment</td>
<td>Double (payment amount)</td>
<td>Loan payments, savings deposits</td>
</tr>
<tr>
<td><strong><code>IPmt</code></strong></td>
<td>Interest portion of payment</td>
<td>Double (interest amount)</td>
<td>Amortization schedules, tax deductions</td>
</tr>
<tr>
<td><strong><code>PPmt</code></strong></td>
<td>Principal portion of payment</td>
<td>Double (principal amount)</td>
<td>Tracking loan balance reduction</td>
</tr>
<tr>
<td><strong>PV</strong></td>
<td>Present value</td>
<td>Double (current value)</td>
<td>Reverse calculation from payment</td>
</tr>
<tr>
<td><strong>FV</strong></td>
<td>Future value</td>
<td>Double (future value)</td>
<td>Investment growth projections</td>
</tr>
<tr>
<td><strong><code>NPer</code></strong></td>
<td>Number of periods</td>
<td>Double (period count)</td>
<td>How long to pay off debt</td>
</tr>
<tr>
<td><strong>Rate</strong></td>
<td>Interest rate</td>
<td>Double (rate per period)</td>
<td>Finding effective interest rate</td>
</tr>
</tbody>
</table>
<h2 id="platform-and-version-notes">Platform and Version Notes</h2>
<ul>
<li>Available in all versions of VBA and VB6</li>
<li>Behavior is consistent across Windows platforms</li>
<li>Uses standard annuity formulas from financial mathematics</li>
<li>For zero interest rates, payment is simply pv / nper</li>
<li>Maximum precision limited by Double data type</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Assumes constant interest rate over entire period</li>
<li>Assumes equal payment amounts (no variable payments)</li>
<li>Does not account for taxes, fees, or insurance directly</li>
<li>Cannot handle variable rate loans (ARMs) without recalculation</li>
<li>Does not consider payment frequency other than what you specify</li>
<li>Sign convention can be confusing (negative for outflows)</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>IPmt</code>: Returns the interest payment for a specific period</li>
<li><code>PPmt</code>: Returns the principal payment for a specific period</li>
<li><code>PV</code>: Returns the present value of an investment</li>
<li><code>FV</code>: Returns the future value of an investment</li>
<li><code>NPer</code>: Returns the number of periods for an investment</li>
<li><code>Rate</code>: Returns the interest rate per period</li>
</ul>
        </article>
        
        <div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
            <p>
                <a href="index.html">← Back to Financial</a> |
                <a href="../index.html">View all functions</a>
            </p>
        </div>

    </main>

    <footer>
        <div class="container">
            <p>&copy; 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
        </div>
    </footer>
</body>
</html>