[英]Loops in Excel VBA -- How do I iterate a function through a range?
So I know Excel has an NPV function, but I'm trying to make my own since I'm trying to teach myself VBA.所以我知道 Excel 有一个 NPV 函数,但我正在尝试创建自己的函数,因为我正在尝试自学 VBA。 I have data laid out in a table and then I want to iterate through a horizontal range of cells (G5 through I5).
我在表格中列出了数据,然后我想遍历单元格的水平范围(G5 到 I5)。 The way I'm trying to structure the loop is by prompting for the number of cash flows and setting that number as the end date.
我试图构建循环的方法是提示输入现金流量并将该数字设置为结束日期。 This way the Macro can be used for problems with different durations.
这样宏可以用于不同持续时间的问题。
My code is not working and I'm not sure why.我的代码不起作用,我不知道为什么。 Can someone take a look and help me out?
有人可以看看并帮助我吗? Please use layman's terms as I just started with VBA yesterday.
请使用外行的术语,因为我昨天刚开始使用 VBA。
Sub NPV()
Dim y As Integer, inv As Double, tax As Double, sal As Double, wc As Double, dr As Double, i As Integer, v As Double
y = Range("C4")
inv = Range("C5")
tax = Range("C6")
sal = Range("E4")
wc = Range("E5")
dr = Range("E6")
Dim total As Double
total = 0
For i = 1 To y
v = Range("5, 5 + i")
v = v / (1 + dr) ^ i
total = total + v
Next i
total = total + ((1 - tax) * sal) + wc
Range("F5").Value = total
End Sub
Consider replacing:考虑更换:
v = Range("5, 5 + i")
with:和:
v = Cells(5, 5 + i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.