Excel自带的NPV和IRR要求等长的时间间隔,一般为年。
我重写的这两个公式以天为单位。
cf是现金流,是一个范围
d是现金流发生的日期,也是一个范围
present是当前日期
r是折现率
MyNPV_是MyNPV的导数
用牛顿迭代公式求IIR
Function MyNPV(cf, d, present, r)
If cf.Rows.Count = d.Rows.Count Then
For i = 1 To cf.Rows.Count
t = (d.Cells(i, 1) - present) / 365
MyNPV = MyNPV + cf.Cells(i, 1) / (1 + r) ^ t
Next
End If
End Function
Function MyNPV_(cf, d, present, r)
If cf.Rows.Count = d.Rows.Count Then
For i = 1 To cf.Rows.Count
t = (d.Cells(i, 1) - present) / 365
MyNPV_ = MyNPV_ - cf.Cells(i, 1) * t / (1 + r) ^ (t + 1)
Next
End If
End Function
Function MyIRR(cf, d, guess)
x2 = guess
Do
x1 = x2
x2 = x1 - MyNPV(cf, d, d.Cells(1, 1), x1) / MyNPV_(cf, d, d.Cells(1, 1), x1)
Loop Until Abs(x2 - x1) < 0.0000001
MyIRR = x2
End Function