950 Chapter 39
Running the macro produces the following in quick succession. Click
OK to move from one message box to the next.
Sub DynPV()
Dim n As Integer ‘ Number of periods
Dim CF() As Double ‘ Dynamic array for cash
fl ows
Dim Temp As Double
Dim i As Integer
n = InputBox(“Enter number of periods”, _
“Present value calculator”, 3)
ReDim CF(1 To n) ‘ redimension the array
‘ for demonstration purpose only
MsgBox “Index starts at: “ & LBound(CF) & _
“ and stops at: “ & UBound(CF), _
vbInformation, “Present value calculator”
For i = 1 To n
CF(i) = InputBox(“Enter value for period “ & _
i, “Present value calculator”, 0)
Next i
Temp = 0
For i = 1 To n
Temp = Temp + CF(i) / 1.05 ^ i
Next i
MsgBox “Present value is: “ & Temp,
vbInformation, _
“Present value calculator”
End Sub
The following macro calculates the present value of a series of future
cash fl ows. To simplify the macro, the interest rate is fi xed at 5 percent
per period. The macro illustrates the use of a dynamic array (the variable
CF) that derives its size from user input (the variable n).