嵌套循环的求解器替代方案

时间:2018-08-17 20:02:50

标签: vba excel-vba nested-loops exit rate

这是我之前发布的问题的重申。

我以根本不需要使用求解器的方式重组了代码。我创建了一个嵌套的For循环,并在外部循环中,将迭代作为所需的变量,并且在我看来,整个算法比使用求解器要简单得多,因为我知道该变量可能具有的最小值和最大值。

我在循环出口设置了一个限制条件,而不是一个相等条件,因为我意识到该值可能永远不会完全相同。此外,我将外循环的原始计数器从非整数值修改为标准形式,并在循环内进行了必要的更改。

我不再遇到编译器错误,但是我的函数输出表明参数之一不在正确的数据类型中。样本数据如下

在此问题上的任何帮助,我都将非常感谢。我在这做错什么了?

测试值(“ Zero_Curve”是一组工作表范围的值):

Clean_Price =  30,343,540
Number_of_Payments = 4
Coupon = 2.80%
Face = 30,000,000
Zero_Curve = [-0.35%   -0.28%   -0.14%   0.02%] (starting at index 1 for the code), with the output of the function close to 0.08% 

代码:

Function ZS(Clean_Price As Double, Number_of_Payments As Integer, Coupon As Double, Face As Double, AccIn As Double, Zero_Curve As Range) As Variant
Dim j As Integer
For j = 0 To 2000 Step 1
Dim SP As Double
Dim i As Integer
For i = 0 To Number_of_Payments
   Dim Payment() As Double
   ReDim Payment(0 To Number_of_Payments) As Double
   Dim Disc() As Double
   ReDim Disc(0 To Number_of_Payments) As Double
   Dim Discp() As Double
   ReDim Discp(0 To Number_of_Payments)
   Select Case i

   Case 0
   Payment(i) = AccIn
   Disc(i) = 1
   Discp(i) = Payment(i) * Disc(i)

   Case Number_of_Payments - 1
   Payment(i) = (AccIn / Coupon) + (Coupon * Face)
   Disc(i) = 1 / (1 + Zero_Curve(i) + ((j - 1000) / 10000)) ^ i
   Discp(i) = Payment(i) * Disc(i)

   Case Number_of_Payments
   Payment(i) = ((Coupon * Face) - (AccIn)) + (((Coupon * Face) - (AccIn)) / Coupon)
   Disc(i) = 1 / (1 + Zero_Curve(i) + ((j - 1000) / 10000)) ^ i
   Discp(i) = Payment(i) * Disc(i)

   Case Else
   Payment(i) = Coupon * Face
   Disc(i) = 1 / (1 + Zero_Curve(i) + ((j - 1000) / 10000)) ^ i
   Discp(i) = Payment(i) * Disc(i)
End Select
Next i
SP = Application.WorksheetFunction.Sum(Discp())
If Abs((SP / (Clean_Price + AccIn)) - 1) < 0.00001 Then Exit For
Next j
ZS = (j - 1000) / 10000

End Function

0 个答案:

没有答案