目标寻求退出年度无限上升

时间:2014-11-07 16:36:09

标签: vba excel-vba excel

我是VBA的新手。我有一个基本的excel 2010项目融资模型,我专门为实践目标寻求宏和学习一些VBA。我使用了这个宏(取自其他人制作的工作簿),并成功地将其改编为我自己的,通过输入所需的IRR来找到PPA费率。

但是,我试图通过输入所需的IRR再次调整它以找到退出年份,并且它导致退出年度无限增加。理想情况下,我希望它只能在指定的范围内寻找一年而且只能寻找一些数字(即仅1-25年)

Private Sub CommandButton2_Click()


Equity_IRR_New = Range("D21").Value
Equity_IRR_Old = Range("C23").Value
Tariff_New = Worksheets("Sheet1").Range("C21").Value
Tariff_Old = 0
'Worksheets("Sheet1").Range("C21").Value = 100000'
If (Equity_IRR_New < Equity_IRR_Old) Then
    Do Until (Equity_IRR_New >= Equity_IRR_Old)
        ExitYr_Old = Worksheets("Sheet1").Range("C21").Value
        ExitYr_New = ExitYr_Old * (0.999)
        Worksheets("Sheet1").Range("C21").Value = ExitYr_New
        Equity_IRR_Old = Range("C23").Value
    Loop
End If

If (Equity_IRR_New > Equity_IRR_Old) Then
    Do Until (Equity_IRR_New <= Equity_IRR_Old)
        ExitYr_Old = Worksheets("Sheet1").Range("C21").Value
        ExitYr_New = ExitYr_Old * (1.001)
        Worksheets("Sheet1").Range("C21").Value = ExitYr_New
        Equity_IRR_Old = Range("C23").Value
    Loop
End If

Range("C21").Value = ExitYr_New


End Sub

提前谢谢

1 个答案:

答案 0 :(得分:0)

问题是因为您的循环条件正在寻找Equity_IRR_New <= Equity_IRR_Old,并且由于您在每次循环迭代中增加Equity_IRR_New,因此永远不会满足该条件。

根据您的评论&#34;&#39;工作表(&#34; Sheet1&#34;)进行设置的方式。范围(&#34; C21&#34;)。值= 100000&#39 ;&#34;,它只会进入第二个&#34;如果&#34;块,即:

If (Equity_IRR_New > Equity_IRR_Old) Then
    Do Until (Equity_IRR_New <= Equity_IRR_Old)
        ExitYr_Old = Worksheets("Sheet1").Range("C21").Value
        ExitYr_New = ExitYr_Old * (1.001)
        Worksheets("Sheet1").Range("C21").Value = ExitYr_New
        Equity_IRR_Old = Range("C23").Value
    Loop
End If

正如我上面所说,Equity_IRR_New的值随着每个循环而不断增加,因为语句为ExitYr_New = ExitYr_Old * (1.001),因此,如果它不小于或等于{{Equity_IRR_Old 1}}开始,它永远不会。单元格中的初始值是什么&#34; C23&#34;?