[英]Goal seek exit year rises infinitely
I am brand new to VBA. 我是VBA的新手。 I have a basic excel 2010 project finance model I made specifically for practicing goal seek macros and learning some VBA.
我有一个基本的Excel 2010项目财务模型,我专门针对练习目标搜索宏和学习一些VBA而制作的。 I used this macro (taken from a workbook made by someone else) and successfully adapted it to my own to find the PPA rate by entering the desired IRR.
我使用了这个宏(取自其他人的工作簿),并通过输入所需的IRR成功地对其进行了调整,以找到PPA汇率。
However, I am trying to adapt it again to find the exit year by entering desired IRR, and it causes the exit year to increase infinitely. 但是,我试图通过输入所需的IRR再次对其进行调整以找到退出年份,这会导致退出年份无限增加。 Ideally I want it to only be able to seek for a year within a specified range and only round numbers (ie years 1-25 only)
理想情况下,我希望它只能搜索指定范围内的一年并且只能取整数(即仅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
Thank you in advance 先感谢您
The problem is because your loop condition is looking for Equity_IRR_New <= Equity_IRR_Old
, and that condition will never be met since you're INCREASING Equity_IRR_New
in each loop iteration. 问题是因为您的循环条件正在寻找
Equity_IRR_New <= Equity_IRR_Old
,并且由于您在每次循环迭代中都Equity_IRR_New
,所以永远不会满足该条件。
The way you have it setup based on your comment "'Worksheets("Sheet1").Range("C21").Value = 100000'", it will only enter the second "if" block, ie: 根据注释“'Worksheets(“ Sheet1”)。Range(“ C21”)。Value = 100000'“进行设置的方式,它将仅输入第二个” 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
As I said above, the value of Equity_IRR_New
is ever increasing with every loop, because of the statement ExitYr_New = ExitYr_Old * (1.001)
, and therefore, if it's not less than or equal to Equity_IRR_Old
to start of with, it will never be. 就像我在上面说的
Equity_IRR_New
,由于语句ExitYr_New = ExitYr_Old * (1.001)
,因此Equity_IRR_New
的值在每个循环中Equity_IRR_New
在不断增加,因此,如果它不小于等于Equity_IRR_Old
,它将永远不会。 What is the initial value in cell "C23"? 单元格“ C23”中的初始值是多少?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.