Arrays in VBA -- PV cash flows

jdmc45

Board Regular
Joined
May 8, 2011
Messages
146
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have an issue with creating a simple function in VBA. The help isn't of much use. I know I am simply missing some key concept in VBA which I'm sure is easy.

I am trying to create a function to calculate the present value of future cash flows, simply CF(t)/(1+r)^(t) for all t. The arrays are the issue. I have many more problems in this format (with vectors and matrices) so if I figure out this one it will carry forward to the others.

Please look at the code below and see if you can help me. Thanks.

Code:
Function PV_discrete(Dim CFtimes(1 To n) As Integer, Dim CFamounts(1, X) As Integer, Dim r As Double)
Dim t() As Integer
PV_discrete = 0
For t = 0 To Length(CFamounts(1, X))
PV_discrete = CFamounts(t) / (1 + r) ^ (CFtimes(t))
Next t
PV_discrete
End Function
 
Yes, it's absolutely correctly!

As the case, CFtimes can be used as an optional argument to meet both conditions:
Rich (BB code):
' This function can be used as UDF in cell formula:
' Excel formula: =PV_discrete(C2,B2:B11)          - for integer CFtimes
' Excel formula: =PV_discrete(C2,B2:B11,A2:A11)   - for floating CFtimes
' Vba call: x = PV_discrete(r, CFamounts)         - for integer CFtimes
' Vba call: x = PV_discrete(r, CFamounts,CFtimes) - for floating CFtimes
Function PV_discrete(r As Double, CFamounts As Range, Optional CFtimes As Range) As Double
 Dim i&, j#, UseTimes As Boolean, Times, Amounts
 Amounts = CFamounts.Value
 If Not CFtimes Is Nothing Then
   UseTimes = True
   Times = CFtimes.Value
 End If
 If Not IsArray(Amounts) Then PV_discrete = Amounts / (1 + r) ^ IIf(UseTimes, Times, 1): Exit Function
 If UseTimes Then
   If UBound(Times) <> UBound(Amounts) Then Exit Function
 End If
 For i = 1 To UBound(Amounts)
   If UseTimes Then j = Times(i, 1) Else j = j + 1
   PV_discrete = PV_discrete + Amounts(i, 1) / (1 + r) ^ j
 Next
End Function
 
' Vba calling: x = PV_discrete(r, CFamounts[, CFtimes ])
Sub Test()
 Dim CFamounts As Range, CFtimes As Range
 Dim r As Double, x As Double
 Set CFtimes = Worksheets("Bond").Range("A2:A101")
 Set CFamounts = Worksheets("Bond").Range("B2:B101")
 r = Worksheets("Bond").Range("C2")
 
 'x = PV_discrete(r, CFamounts)          ' <-- Test one
 x = PV_discrete(r, CFamounts, CFtimes)  ' <-- of these lines
 
 Worksheets("Bond").Range("D2").Value = x
End Sub

Cheers :beerchug:
Awesome, thanks
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top