VBA循环。我不明白循环的目的

时间:2019-03-03 16:18:01

标签: vba

所以我对此有一个解决方案:

创建一个函数(使用For Next),该函数计算从一次性支出到n年的银行帐户中所得的收入。在提供的工作簿中,€1,000在5年后以5%的收入可获得€276。在单元格E5中测试您的功能。

这是代码:

Public Function calprofit(investment As Single, years As Integer, rate As Single) As Single
Dim i As Integer

Dim amount As Single

amount = investment

For i = 1 To years
amount = amount * (1 + rate)
Next i

calprofit = amount - investment

End Function

我不太了解这里发生了什么。尤其是上面说的部分

for i = 1 to years 

amount = amount * (1 + rate)

感谢帮助和耐心。

2 个答案:

答案 0 :(得分:0)

for循环在循环持续时间内一次又一次地在循环内执行代码,其中变量import UIKit class TableViewController: UITableViewController { // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return 1 // Hardcoded in this example. } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 40 // Hardcoded in this example. } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if let customCell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as? CustomCell { // Logic around which option to display if indexPath.row < 20 { customCell.option = .star } else { customCell.option = .tree } return customCell } // Fallback. return UITableViewCell() } } 在这种情况下从1更改为年数,并且介于两者之间。

在该循环中,金额乘以1 +利率。假设比率为5%,则每次循环中都会添加5%的比率。

但是我不明白为什么要为此使用自定义函数。 Excel中有很多财务公式可以计算这些事情,您可以为此使用Excel公式:i

答案 1 :(得分:0)

i和数量的含义是,每次执行循环后变量都会更改。

在您的情况下,年总数为5,这意味着循环最多运行5次。 您的起始金额为1000。经过1年的5%利息(第一个循环),等于1050。第二次(i现在等于5中的2)起始金额为1050,现在计算出1050的5%。 ,以此类推,直到5次,总计您的利息€276。

希望这可以使您更清楚:)