我正在寻找一个类似于微软NPV功能的功能,但有一些关键的区别:
从时间段0开始(意味着第一个值不打折)
它允许折扣率的数组来计算一段时间内的费率变化
我写的函数如下,然而,这是非常低效的。保存文件花了我几分钟 - 所以我需要一种新方法,但我看不到在VBA中进行批量计算的方法。非常感谢任何帮助。
Public Function NPV_UDF(disc_rng As Range, disc_rate As Variant)
'disc_rng - range of cash flows to be discounted and summed
'disc_rate - discount rate, can either be a single value, or a range of rates matched to the range of cash flows
'range_NPV will be the running sum of the discounted cash flows
range_NPV = 0
'If the disc_rate is a range larger than 1 cell, check to make
'sure it matches the dimension of the cash flow range (disc_rng)
If TypeName(disc_rate) = "Range" Then
If disc_rate.Columns.Count > 1 Then
If disc_rate.Columns.Count <> disc_rng.Columns.Count Then
NPV_UDF = -1
Exit Function
End If
'iterate over the cash flows (disc_rng), discount and update total
For col_idx = 1 To disc_rng.Columns.Count Step 1
range_NPV = range_NPV + (disc_rng.Cells(1, col_idx).Value / ((1 + disc_rate.Cells(1, col_idx).Value) ^ (col_idx - 1)))
Next
Else
'In this conditional only a single discount rate value was specified
'iteration will only be over the cash flows range (disc_rng) and
'the discount rate value will remain constant
For col_idx = 1 To disc_rng.Columns.Count Step 1
range_NPV = range_NPV + (disc_rng.Cells(1, col_idx).Value / ((1 + disc_rate.Value) ^ (col_idx - 1)))
Next
End If
Else
'In this conditional, the discount rate (disc_rate) was some other type than a range (e.g., a hard-coded number was entered)
'as such the same approach is taken where the discount rate
'is held constant and the function discounts the cash flows and totals them
For col_idx = 1 To disc_rng.Columns.Count Step 1
range_NPV = range_NPV + (disc_rng.Cells(1, col_idx).Value / ((1 + disc_rate) ^ (col_idx - 1)))
Next
End If
NPV_UDF = range_NPV
End Function`