I am trying to write a vba macro that calls xirr on an array constructed from arguments. However it always return 0. It works perfectly if I have a cell formula with xirr on the exact same data.
A somewhat minimal version of the macro follows:
Function myXirr(startVal, startDate, deltaVal, deltaStart, deltaDays, numInt, endVal, endDate, est) As Double
Dim dates() As Date
Dim vals() As Double
ReDim dates(numInt + 2)
ReDim vals(numInt + 2)
dates(1) = startDate
vals(1) = startVal
dates(2) = deltaStart
vals(2) = deltaVal
For i = 2 To numInt
dates(i + 1) = dates(i) + deltaDays
vals(i + 1) = deltaVal
Next i
dates(numInt + 2) = endDate
vals(numInt + 2) = -endVal
myXirr = [atpvbaen.xls].XIrr(vals, dates, est)
End Function
I am calling it on the following spreadsheet -
1-Jan |
1000 |
|
1-Feb |
100 |
|
1-Mar |
100 |
=myXirr(-B1,A1,B2,A2,30,2,B4,A4,0.1) |
1-Apr |
-1233.31 |
=Xirr(b1:b4,a1:a4) |
The Xirr cell correctly shows .127 but the myXirr always shows 0 regardless of its arguments (I know the 2 shouldn't be exactly the same in my simplified version with the 30 argument above, but they should be close enough).
Thanks for any help.
Bookmarks