如何改进DCF模型的原始VBA代码?

我是VBA的新手,我一直在试图创build一个程序,使我能够以一定的折扣率获得一个起始现金数额的折扣,并且将折扣率乘以n年来find一个给定的贴现现金基于给定的年数的stream量。 有没有人有任何提示,我怎样才能使这个代码更有效率? 另外,使用循环汇总不同数额的现金,如“(100 / 1.01)+(100 / 1.01 ^ 2)+(100 / 1.01 ^ 3)”有什么build议? 提前致谢!

Sub DCF() Dim z As Double, CashFlow As Double, DiscRate As Double, Periods As Double CashFlow = InputBox("Enter initial cash flow: ", "Cash Flow") DiscRate = InputBox("Enter discount rate in decimal form: ", "Discount Rate") Periods = InputBox("How many periods (in years) are there?", "Periods") z = CashFlow / (1 + DiscRate) ^ Periods MsgBox Format(z, "$0.00") End Sub 

您拥有的代码没有问题,只需要一个循环来创build贴现现金stream量。 这应该做你想要的:

 Sub DCF() Dim z As Double, CashFlow As Double, DiscRate As Double, Periods As Double: z = 0# CashFlow = InputBox("Enter initial cash flow: ", "Cash Flow") DiscRate = InputBox("Enter discount rate in decimal form: ", "Discount Rate") Periods = InputBox("How many periods (in years) are there?", "Periods") Dim i As Integer For i = 1 To Periods: z = z + CashFlow / (1# + DiscRate) ^ i Next MsgBox Format(z, "$0.00") End Sub 
Interesting Posts