我使用循环来计算每年增加15%后的结束租金。 这很好。
Private Sub Worksheet_Change(ByVal Target As range)
On Error GoTo ErrorHandler '<--| be sure to catch any error and enable events handling back
Select Case Target.Address
Case "$F$15"
'Recalculate as necessary Using Looping
amount = range("F12").Value
For counter = 1 To range("F15").Value
Debug.Print counter;
amount = amount + (range("F12").Value * 0.15)
Next
range("F16").Value = amount
对我来说,问题是我希望有相同类型的循环来找出相反的情况。我希望能够根据输入的年份弄清楚初始租金是多少。 (F21)假设我们知道结束租金。 LLVM Programmer Manual
所以需要弄清楚F20。 我问我的儿子,他用代数来解决这个问题,但是我无法帮助我学习excel。 感谢您的任何建议。
答案 0 :(得分:2)
我使用循环方法和非循环方法重构了代码:
Private Sub Worksheet_Change(ByVal Target As range)
On Error GoTo ErrorHandler '<--| be sure to catch any error and enable events handling back
Dim factor As Double
Dim period As Integer
Dim amount As Double
Select Case Target.Address
Case "$F$15"
period = Range("F15").Value
amount = Range("F12").Value
'Recalculate as necessary Using Looping
factor = 1
For counter = 1 To period
Debug.Print counter;
factor = factor + 0.15
Next
Range("F16").Value = amount * factor
'alternate method without looping
Range("F16").Value = amount * (1 + period * 0.15)
'which can also be written in VBA as
[F16] = [F12] * (1 + [F15] * 0.15)
Case "$F$21"
period = Range("F21").Value
amount = Range("F19").Value
'Recalculate as necessary Using Looping
factor = 1
For counter = 1 To period
factor = factor + 0.15
Next
Range("F20").Value = amount / factor
'alternate method without looping
Range("F20").Value = amount / (1 + period * 0.15)
'which can also be written in VBA as
[F20] = [F19] / (1 + [F21] * 0.15)