vb6parse 1.0.1

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
<!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 - pv - Financial">
    <title>pv - 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> / pv</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="pv-function">PV Function</h1>
<p>Returns a Double specifying the present value of an annuity based on periodic, fixed payments to be paid in the future and a fixed interest rate.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">PV(rate, nper, pmt, [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>pmt</code> - Required. Double specifying payment to be made each period. Payments usually contain principal and interest that does not change over the life of the annuity.</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 present value of an annuity. The present value is the current value of a series of future payments or the current value of a future lump sum.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>PV</code> function is the inverse of the <code>FV</code> function. While <code>FV</code> calculates what a series of payments will be worth in the future,
<code>PV</code> calculates what those same future payments are worth today, discounted by a rate of return.
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 Uses</strong>:
- <strong>Loan Affordability</strong>: Calculate how much you can borrow given a specific payment amount
- <strong>Investment Valuation</strong>: Determine current value of future cash flows
- <strong>Annuity Pricing</strong>: Calculate lump sum value of periodic payments
- <strong>Lease Analysis</strong>: Determine present value of lease payments</p>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Loan Affordability</strong>: Calculate maximum loan amount based on affordable payment</li>
<li><strong>Investment Valuation</strong>: Determine present value of future investment returns</li>
<li><strong>Annuity Valuation</strong>: Calculate lump sum value of annuity payments</li>
<li><strong>Bond Pricing</strong>: Value bonds based on coupon payments and face value</li>
<li><strong>Lease vs Buy Analysis</strong>: Compare present value of lease payments to purchase price</li>
<li><strong>Pension Valuation</strong>: Calculate current value of future pension payments</li>
<li><strong>Structured Settlement</strong>: Determine lump sum value of periodic payments</li>
<li><strong>Capital Budgeting</strong>: Evaluate present value of project cash flows</li>
</ol>
<h2 id="basic-examples">Basic Examples</h2>
<h3 id="example-1-loan-affordability">Example 1: Loan Affordability</h3>
<pre><code class="language-vbnet">&#x27; How much can you borrow if you can afford $500/month for 5 years at 6% APR?
Dim loanAmount As Double
loanAmount = Abs(PV(0.06 / 12, 5 * 12, -500))
&#x27; Returns approximately $25,775 (negative payment = money you pay out)</code></pre>
<h3 id="example-2-investment-present-value">Example 2: Investment Present Value</h3>
<pre><code class="language-vbnet">&#x27; What&#x27;s the present value of receiving $1,000/month for 10 years at 5% return?
Dim presentValue As Double
presentValue = Abs(PV(0.05 / 12, 10 * 12, 1000))
&#x27; Returns approximately $94,289 (positive payment = money you receive)</code></pre>
<h3 id="example-3-annuity-valuation">Example 3: Annuity Valuation</h3>
<pre><code class="language-vbnet">&#x27; Value of annuity paying $2,000/month for 20 years at 4% discount rate
Dim annuityValue As Double
annuityValue = Abs(PV(0.04 / 12, 20 * 12, 2000))
&#x27; Returns the lump sum equivalent value</code></pre>
<h3 id="example-4-lump-sum-with-future-value">Example 4: Lump Sum with Future Value</h3>
<pre><code class="language-vbnet">&#x27; Present value of $50,000 in 10 years at 6% annual return (no periodic payments)
Dim presentValue As Double
presentValue = Abs(PV(0.06, 10, 0, -50000))
&#x27; Returns approximately $27,920 (what you&#x27;d need to invest today)</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="pattern-1-calculateaffordableloan">Pattern 1: <code>CalculateAffordableLoan</code></h3>
<pre><code class="language-vbnet">Function CalculateAffordableLoan(monthlyPayment 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; Negative payment because it&#x27;s money flowing out
    CalculateAffordableLoan = Abs(PV(monthlyRate, numPayments, -monthlyPayment))
End Function</code></pre>
<h3 id="pattern-2-comparepaymentoptions">Pattern 2: <code>ComparePaymentOptions</code></h3>
<pre><code class="language-vbnet">Function ComparePaymentOptions(payment1 As Double, years1 As Integer, _
                               payment2 As Double, years2 As Integer, _
                               rate As Double) As String
    Dim pv1 As Double
    Dim pv2 As Double
    pv1 = Abs(PV(rate / 12, years1 * 12, -payment1))
    pv2 = Abs(PV(rate / 12, years2 * 12, -payment2))
    If pv1 &gt; pv2 Then
        ComparePaymentOptions = &quot;Option 1 allows borrowing $&quot; &amp; _
                               Format(pv1 - pv2, &quot;#,##0&quot;) &amp; &quot; more&quot;
    Else
        ComparePaymentOptions = &quot;Option 2 allows borrowing $&quot; &amp; _
                               Format(pv2 - pv1, &quot;#,##0&quot;) &amp; &quot; more&quot;
    End If
End Function</code></pre>
<h3 id="pattern-3-calculatelumpsumequivalent">Pattern 3: <code>CalculateLumpSumEquivalent</code></h3>
<pre><code class="language-vbnet">Function CalculateLumpSumEquivalent(monthlyPayment As Double, _
                                    years As Integer, _
                                    discountRate As Double) As Double
    &#x27; Calculate what a stream of payments is worth as a lump sum today
    Dim monthlyRate As Double
    monthlyRate = discountRate / 12
    CalculateLumpSumEquivalent = Abs(PV(monthlyRate, years * 12, monthlyPayment))
End Function</code></pre>
<h3 id="pattern-4-validatepvparameters">Pattern 4: <code>ValidatePVParameters</code></h3>
<pre><code class="language-vbnet">Function ValidatePVParameters(rate As Double, nper As Integer, _
                              pmt As Double) As Boolean
    ValidatePVParameters = False
    If nper &lt;= 0 Then
        MsgBox &quot;Number of periods must be positive&quot;
        Exit Function
    End If
    If rate &lt; -1 Then
        MsgBox &quot;Interest rate cannot be less than -100%&quot;
        Exit Function
    End If
    ValidatePVParameters = True
End Function</code></pre>
<h3 id="pattern-5-calculatebreakevenloanamount">Pattern 5: <code>CalculateBreakEvenLoanAmount</code></h3>
<pre><code class="language-vbnet">Function CalculateBreakEvenLoanAmount(payment As Double, _
                                      rate As Double, _
                                      years As Integer, _
                                      upfrontCosts As Double) As Double
    &#x27; Calculate loan amount where total cost equals upfront costs
    Dim loanPV As Double
    loanPV = Abs(PV(rate / 12, years * 12, -payment))
    CalculateBreakEvenLoanAmount = loanPV - upfrontCosts
End Function</code></pre>
<h3 id="pattern-6-pvofmixedcashflows">Pattern 6: <code>PVOfMixedCashFlows</code></h3>
<pre><code class="language-vbnet">Function PVOfMixedCashFlows(regularPayment As Double, _
                            rate As Double, _
                            nper As Integer, _
                            futureValue As Double) As Double
    &#x27; Calculate PV when you have both regular payments and a lump sum
    PVOfMixedCashFlows = Abs(PV(rate, nper, regularPayment, futureValue))
End Function</code></pre>
<h3 id="pattern-7-calculaterequireddownpayment">Pattern 7: <code>CalculateRequiredDownPayment</code></h3>
<pre><code class="language-vbnet">Function CalculateRequiredDownPayment(homePrice As Double, _
                                      monthlyPayment As Double, _
                                      rate As Double, _
                                      years As Integer) As Double
    Dim maxLoan As Double
    maxLoan = Abs(PV(rate / 12, years * 12, -monthlyPayment))
    If maxLoan &gt;= homePrice Then
        CalculateRequiredDownPayment = 0
    Else
        CalculateRequiredDownPayment = homePrice - maxLoan
    End If
End Function</code></pre>
<h3 id="pattern-8-comparelumpsumvsannuity">Pattern 8: <code>CompareLumpSumVsAnnuity</code></h3>
<pre><code class="language-vbnet">Function CompareLumpSumVsAnnuity(lumpSum As Double, _
                                 annuityPayment As Double, _
                                 years As Integer, _
                                 discountRate As Double) As String
    Dim annuityPV As Double
    Dim difference As Double
    annuityPV = Abs(PV(discountRate / 12, years * 12, annuityPayment))
    difference = lumpSum - annuityPV
    If difference &gt; 0 Then
        CompareLumpSumVsAnnuity = &quot;Lump sum is better by $&quot; &amp; Format(difference, &quot;#,##0&quot;)
    ElseIf difference &lt; 0 Then
        CompareLumpSumVsAnnuity = &quot;Annuity is better by $&quot; &amp; Format(Abs(difference), &quot;#,##0&quot;)
    Else
        CompareLumpSumVsAnnuity = &quot;Both options are equal&quot;
    End If
End Function</code></pre>
<h3 id="pattern-9-calculateleasevalue">Pattern 9: <code>CalculateLeaseValue</code></h3>
<pre><code class="language-vbnet">Function CalculateLeaseValue(monthlyLease As Double, _
                             leaseTermMonths As Integer, _
                             discountRate As Double) As Double
    &#x27; Calculate present value of all lease payments
    CalculateLeaseValue = Abs(PV(discountRate / 12, leaseTermMonths, -monthlyLease))
End Function</code></pre>
<h3 id="pattern-10-findaffordablepayment">Pattern 10: <code>FindAffordablePayment</code></h3>
<pre><code class="language-vbnet">Function FindAffordablePayment(desiredLoan As Double, _
                               rate As Double, _
                               nper As Integer) As Double
    &#x27; Reverse calculation: find payment from desired loan amount
    &#x27; This uses Pmt, but demonstrates PV relationship
    FindAffordablePayment = Abs(Pmt(rate, nper, desiredLoan))
End Function</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="example-1-comprehensive-loan-calculator">Example 1: Comprehensive Loan Calculator</h3>
<pre><code class="language-vbnet">&#x27; Calculate loan amounts based on payment affordability
Class LoanAffordabilityCalculator
    Private m_monthlyIncome As Double
    Private m_monthlyDebts As Double
    Private m_annualRate As Double
    Private m_loanYears As Integer
    Private m_debtToIncomeRatio As Double
    Public Sub Initialize(monthlyIncome As Double, monthlyDebts As Double, _
                         annualRate As Double, loanYears As Integer)
        m_monthlyIncome = monthlyIncome
        m_monthlyDebts = monthlyDebts
        m_annualRate = annualRate
        m_loanYears = loanYears
        m_debtToIncomeRatio = 0.43  &#x27; Standard 43% DTI ratio
    End Sub
    Public Function GetMaxMonthlyPayment() As Double
        Dim maxTotalDebt As Double
        Dim maxPayment As Double
        maxTotalDebt = m_monthlyIncome * m_debtToIncomeRatio
        maxPayment = maxTotalDebt - m_monthlyDebts
        If maxPayment &lt; 0 Then maxPayment = 0
        GetMaxMonthlyPayment = maxPayment
    End Function
    Public Function GetMaxLoanAmount() As Double
        Dim maxPayment As Double
        Dim monthlyRate As Double
        Dim numPayments As Integer
        maxPayment = GetMaxMonthlyPayment()
        monthlyRate = m_annualRate / 12
        numPayments = m_loanYears * 12
        &#x27; Use PV to find how much can be borrowed
        GetMaxLoanAmount = Abs(PV(monthlyRate, numPayments, -maxPayment))
    End Function
    Public Function GetLoanWithDownPayment(downPayment As Double) As Double
        GetLoanWithDownPayment = GetMaxLoanAmount() + downPayment
    End Function
    Public Function GetRequiredDownPayment(homePrice As Double) As Double
        Dim maxLoan As Double
        maxLoan = GetMaxLoanAmount()
        If maxLoan &gt;= homePrice Then
            GetRequiredDownPayment = 0
        Else
            GetRequiredDownPayment = homePrice - maxLoan
        End If
    End Function
    Public Function GenerateAffordabilityReport() As String
        Dim report As String
        Dim maxPayment As Double
        Dim maxLoan As Double
        maxPayment = GetMaxMonthlyPayment()
        maxLoan = GetMaxLoanAmount()
        report = &quot;Loan Affordability Analysis&quot; &amp; vbCrLf
        report = report &amp; String(50, &quot;=&quot;) &amp; vbCrLf
        report = report &amp; &quot;Monthly Income: $&quot; &amp; Format(m_monthlyIncome, &quot;#,##0&quot;) &amp; vbCrLf
        report = report &amp; &quot;Existing Debts: $&quot; &amp; Format(m_monthlyDebts, &quot;#,##0&quot;) &amp; vbCrLf
        report = report &amp; &quot;DTI Ratio: &quot; &amp; Format(m_debtToIncomeRatio * 100, &quot;0&quot;) &amp; &quot;%&quot; &amp; vbCrLf
        report = report &amp; String(50, &quot;-&quot;) &amp; vbCrLf
        report = report &amp; &quot;Max Monthly Payment: $&quot; &amp; Format(maxPayment, &quot;#,##0&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_loanYears &amp; &quot; years&quot; &amp; vbCrLf
        report = report &amp; String(50, &quot;-&quot;) &amp; vbCrLf
        report = report &amp; &quot;Maximum Loan Amount: $&quot; &amp; Format(maxLoan, &quot;#,##0&quot;)
        GenerateAffordabilityReport = report
    End Function
End Class</code></pre>
<h3 id="example-2-annuity-comparison-tool">Example 2: Annuity Comparison Tool</h3>
<pre><code class="language-vbnet">&#x27; Compare different annuity and lump sum options
Module AnnuityComparison
    Private Type AnnuityOption
        Name As String
        Payment As Double
        Years As Integer
        IsLumpSum As Boolean
        LumpSumAmount As Double
    End Type
    Public Function CompareOptions(options() As AnnuityOption, _
                                  discountRate As Double) As String
        Dim report As String
        Dim i As Integer
        Dim pv As Double
        Dim monthlyRate As Double
        Dim bestValue As Double
        Dim bestIndex As Integer
        monthlyRate = discountRate / 12
        bestValue = 0
        bestIndex = LBound(options)
        report = &quot;Annuity Option Comparison&quot; &amp; vbCrLf
        report = report &amp; &quot;Discount Rate: &quot; &amp; Format(discountRate * 100, &quot;0.0&quot;) &amp; &quot;%&quot; &amp; vbCrLf
        report = report &amp; String(60, &quot;=&quot;) &amp; vbCrLf
        report = report &amp; &quot;Option              Type        Present Value&quot; &amp; vbCrLf
        report = report &amp; String(60, &quot;-&quot;) &amp; vbCrLf
        For i = LBound(options) To UBound(options)
            If options(i).IsLumpSum Then
                pv = options(i).LumpSumAmount
            Else
                pv = Abs(PV(monthlyRate, options(i).Years * 12, options(i).Payment))
            End If
            If pv &gt; bestValue Then
                bestValue = pv
                bestIndex = i
            End If
            report = report &amp; Left(options(i).Name &amp; Space(20), 20) &amp; _
                     IIf(options(i).IsLumpSum, &quot;Lump Sum    &quot;, &quot;Annuity     &quot;) &amp; _
                     &quot;$&quot; &amp; Format(pv, &quot;#,##0&quot;)
            If i = bestIndex Then report = report &amp; &quot; *BEST*&quot;
            report = report &amp; vbCrLf
        Next i
        report = report &amp; String(60, &quot;-&quot;) &amp; vbCrLf
        report = report &amp; &quot;Recommended: &quot; &amp; options(bestIndex).Name
        CompareOptions = report
    End Function
    Public Function CalculateAnnuityYield(lumpSum As Double, _
                                         monthlyPayment As Double, _
                                         years As Integer) As Double
        &#x27; Find the discount rate that makes PV equal to lump sum
        &#x27; This is a simplified approximation
        Dim rate As Double
        Dim pv As Double
        Dim diff As Double
        rate = 0.05  &#x27; Starting guess
        Do
            pv = Abs(PV(rate / 12, years * 12, monthlyPayment))
            diff = pv - lumpSum
            If Abs(diff) &lt; 0.01 Then Exit Do
            &#x27; Adjust rate
            If diff &gt; 0 Then
                rate = rate + 0.0001
            Else
                rate = rate - 0.0001
            End If
        Loop While Abs(diff) &gt; 0.01
        CalculateAnnuityYield = rate
    End Function
End Module</code></pre>
<h3 id="example-3-lease-vs-buy-analyzer">Example 3: Lease vs Buy Analyzer</h3>
<pre><code class="language-vbnet">&#x27; Compare leasing vs buying with present value analysis
Class LeaseVsBuyAnalyzer
    Private m_purchasePrice As Double
    Private m_monthlyLease As Double
    Private m_leaseTermMonths As Integer
    Private m_discountRate As Double
    Private m_residualValue As Double
    Public Sub Initialize(purchasePrice As Double, monthlyLease As Double, _
                         leaseTermMonths As Integer, discountRate As Double, _
                         residualValue As Double)
        m_purchasePrice = purchasePrice
        m_monthlyLease = monthlyLease
        m_leaseTermMonths = leaseTermMonths
        m_discountRate = discountRate
        m_residualValue = residualValue
    End Sub
    Public Function GetLeasePresentValue() As Double
        &#x27; Calculate PV of all lease payments
        GetLeasePresentValue = Abs(PV(m_discountRate / 12, m_leaseTermMonths, -m_monthlyLease))
    End Function
    Public Function GetBuyPresentValue() As Double
        &#x27; Calculate PV of buying (purchase price minus PV of residual value)
        Dim pvResidual As Double
        &#x27; PV of residual value (what it&#x27;s worth after lease term)
        pvResidual = Abs(PV(m_discountRate / 12, m_leaseTermMonths, 0, -m_residualValue))
        GetBuyPresentValue = m_purchasePrice - pvResidual
    End Function
    Public Function GetRecommendation() As String
        Dim leasePV As Double
        Dim buyPV As Double
        Dim difference As Double
        leasePV = GetLeasePresentValue()
        buyPV = GetBuyPresentValue()
        difference = Abs(buyPV - leasePV)
        If leasePV &lt; buyPV Then
            GetRecommendation = &quot;LEASE - Saves $&quot; &amp; Format(difference, &quot;#,##0&quot;) &amp; &quot; in PV&quot;
        ElseIf leasePV &gt; buyPV Then
            GetRecommendation = &quot;BUY - Saves $&quot; &amp; Format(difference, &quot;#,##0&quot;) &amp; &quot; in PV&quot;
        Else
            GetRecommendation = &quot;Either option - PV is equal&quot;
        End If
    End Function
    Public Function GenerateAnalysis() As String
        Dim analysis As String
        Dim leasePV As Double
        Dim buyPV As Double
        leasePV = GetLeasePresentValue()
        buyPV = GetBuyPresentValue()
        analysis = &quot;Lease vs Buy Analysis&quot; &amp; vbCrLf
        analysis = analysis &amp; String(50, &quot;=&quot;) &amp; vbCrLf
        analysis = analysis &amp; &quot;Purchase Price: $&quot; &amp; Format(m_purchasePrice, &quot;#,##0&quot;) &amp; vbCrLf
        analysis = analysis &amp; &quot;Monthly Lease: $&quot; &amp; Format(m_monthlyLease, &quot;#,##0&quot;) &amp; vbCrLf
        analysis = analysis &amp; &quot;Lease Term: &quot; &amp; m_leaseTermMonths &amp; &quot; months&quot; &amp; vbCrLf
        analysis = analysis &amp; &quot;Discount Rate: &quot; &amp; Format(m_discountRate * 100, &quot;0.0&quot;) &amp; &quot;%&quot; &amp; vbCrLf
        analysis = analysis &amp; &quot;Residual Value: $&quot; &amp; Format(m_residualValue, &quot;#,##0&quot;) &amp; vbCrLf
        analysis = analysis &amp; String(50, &quot;-&quot;) &amp; vbCrLf
        analysis = analysis &amp; &quot;Lease PV: $&quot; &amp; Format(leasePV, &quot;#,##0&quot;) &amp; vbCrLf
        analysis = analysis &amp; &quot;Buy PV: $&quot; &amp; Format(buyPV, &quot;#,##0&quot;) &amp; vbCrLf
        analysis = analysis &amp; String(50, &quot;-&quot;) &amp; vbCrLf
        analysis = analysis &amp; &quot;Recommendation: &quot; &amp; GetRecommendation()
        GenerateAnalysis = analysis
    End Function
End Class</code></pre>
<h3 id="example-4-pension-valuation-calculator">Example 4: Pension Valuation Calculator</h3>
<pre><code class="language-vbnet">&#x27; Calculate present value of pension benefits
Class PensionValuator
    Private m_monthlyPension As Double
    Private m_yearsToRetirement As Integer
    Private m_yearsOfPayments As Integer
    Private m_discountRate As Double
    Private m_inflationRate As Double
    Public Sub SetPensionDetails(monthlyPension As Double, _
                                yearsToRetirement As Integer, _
                                yearsOfPayments As Integer)
        m_monthlyPension = monthlyPension
        m_yearsToRetirement = yearsToRetirement
        m_yearsOfPayments = yearsOfPayments
    End Sub
    Public Sub SetEconomicAssumptions(discountRate As Double, inflationRate As Double)
        m_discountRate = discountRate
        m_inflationRate = inflationRate
    End Sub
    Public Function GetPensionPresentValue() As Double
        Dim monthlyRate As Double
        Dim numPayments As Integer
        Dim pvAtRetirement As Double
        Dim pvToday As Double
        monthlyRate = m_discountRate / 12
        numPayments = m_yearsOfPayments * 12
        &#x27; Calculate PV at retirement
        pvAtRetirement = Abs(PV(monthlyRate, numPayments, m_monthlyPension))
        &#x27; Discount back to today
        pvToday = Abs(PV(m_discountRate, m_yearsToRetirement, 0, -pvAtRetirement))
        GetPensionPresentValue = pvToday
    End Function
    Public Function GetInflationAdjustedValue() As Double
        Dim realRate As Double
        Dim monthlyRate As Double
        Dim numPayments As Integer
        Dim pvAtRetirement As Double
        Dim pvToday As Double
        &#x27; Fisher equation: (1 + nominal) = (1 + real)(1 + inflation)
        realRate = ((1 + m_discountRate) / (1 + m_inflationRate)) - 1
        monthlyRate = realRate / 12
        numPayments = m_yearsOfPayments * 12
        pvAtRetirement = Abs(PV(monthlyRate, numPayments, m_monthlyPension))
        pvToday = Abs(PV(realRate, m_yearsToRetirement, 0, -pvAtRetirement))
        GetInflationAdjustedValue = pvToday
    End Function
    Public Function GenerateValuationReport() As String
        Dim report As String
        Dim nominalPV As Double
        Dim realPV As Double
        Dim totalPayments As Double
        nominalPV = GetPensionPresentValue()
        realPV = GetInflationAdjustedValue()
        totalPayments = m_monthlyPension * m_yearsOfPayments * 12
        report = &quot;Pension Valuation Report&quot; &amp; vbCrLf
        report = report &amp; String(50, &quot;=&quot;) &amp; vbCrLf
        report = report &amp; &quot;Monthly Pension: $&quot; &amp; Format(m_monthlyPension, &quot;#,##0&quot;) &amp; vbCrLf
        report = report &amp; &quot;Years to Retirement: &quot; &amp; m_yearsToRetirement &amp; vbCrLf
        report = report &amp; &quot;Years of Payments: &quot; &amp; m_yearsOfPayments &amp; vbCrLf
        report = report &amp; &quot;Discount Rate: &quot; &amp; Format(m_discountRate * 100, &quot;0.0&quot;) &amp; &quot;%&quot; &amp; vbCrLf
        report = report &amp; &quot;Inflation Rate: &quot; &amp; Format(m_inflationRate * 100, &quot;0.0&quot;) &amp; &quot;%&quot; &amp; vbCrLf
        report = report &amp; String(50, &quot;-&quot;) &amp; vbCrLf
        report = report &amp; &quot;Total Nominal Payments: $&quot; &amp; Format(totalPayments, &quot;#,##0&quot;) &amp; vbCrLf
        report = report &amp; &quot;Present Value (Nominal): $&quot; &amp; Format(nominalPV, &quot;#,##0&quot;) &amp; vbCrLf
        report = report &amp; &quot;Present Value (Real): $&quot; &amp; Format(realPV, &quot;#,##0&quot;) &amp; vbCrLf
        report = report &amp; String(50, &quot;-&quot;) &amp; vbCrLf
        report = report &amp; &quot;Value Reduction from Inflation: $&quot; &amp; _
                 Format(nominalPV - realPV, &quot;#,##0&quot;) &amp; &quot; (&quot; &amp; _
                 Format(((nominalPV - realPV) / nominalPV) * 100, &quot;0.0&quot;) &amp; &quot;%)&quot;
        GenerateValuationReport = report
    End Function
End Class</code></pre>
<h2 id="error-handling">Error Handling</h2>
<p>The <code>PV</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 values exceed Double range
Always validate input parameters:</p>
<pre><code class="language-vbnet">On Error Resume Next
presentValue = PV(rate, nper, pmt, fv, type)
If Err.Number &lt;&gt; 0 Then
    MsgBox &quot;Error calculating present value: &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>PV</code> function is very fast for individual calculations</li>
<li>Avoid calling repeatedly in tight loops if parameters don't change</li>
<li>Pre-calculate monthly rates and other constants outside loops</li>
<li>For sensitivity analysis, consider caching results</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Convert Rates Properly</strong>: Always divide annual rates by 12 for monthly calculations</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 values to users</li>
<li><strong>Validate Inputs</strong>: Check that nper &gt; 0 and rate is reasonable</li>
<li><strong>Handle Sign Conventions</strong>: Remember negative = outflow, positive = inflow</li>
<li><strong>Round for Display</strong>: Use <code>Format()</code> for currency display</li>
<li><strong>Document Assumptions</strong>: Clearly state discount rates and time periods</li>
<li><strong>Consider Inflation</strong>: Use real rates for inflation-adjusted analysis</li>
<li><strong>Test Edge Cases</strong>: Verify behavior with 0% rate, very long terms</li>
<li><strong>Compare with Pmt</strong>: Understand the inverse relationship between PV and Pmt</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>PV</strong></td>
<td>Present value of annuity</td>
<td>Double (current value)</td>
<td>Loan affordability, investment valuation</td>
</tr>
<tr>
<td><strong>FV</strong></td>
<td>Future value of annuity</td>
<td>Double (future value)</td>
<td>Investment growth, savings goals</td>
</tr>
<tr>
<td><strong>Pmt</strong></td>
<td>Periodic payment</td>
<td>Double (payment amount)</td>
<td>Loan payments, inverse of PV</td>
</tr>
<tr>
<td><strong>NPV</strong></td>
<td>Net present value</td>
<td>Double (NPV)</td>
<td>Project evaluation with irregular cash flows</td>
</tr>
<tr>
<td><strong><code>NPer</code></strong></td>
<td>Number of periods</td>
<td>Double (period count)</td>
<td>Time to goal calculation</td>
</tr>
<tr>
<td><strong>Rate</strong></td>
<td>Interest rate</td>
<td>Double (rate per period)</td>
<td>Finding effective 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 present value formulas from financial mathematics</li>
<li>For zero interest rates, PV is simply pmt * nper + fv</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 (standard annuity)</li>
<li>Does not account for taxes, fees, or transaction costs</li>
<li>Cannot handle variable rate scenarios 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>FV</code>: Returns the future value of an investment</li>
<li><code>Pmt</code>: Returns the periodic payment for an annuity</li>
<li><code>PPmt</code>: Returns the principal payment for a specific period</li>
<li><code>IPmt</code>: Returns the interest payment for a specific period</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>
<li><code>NPV</code>: Returns the net present value with irregular cash flows</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>