我正在尝试反转当前循环的工作方式。目前,我有一组贷款,在移至下一笔贷款之前,循环会对k个周期的每笔贷款进行计算。我需要扭转这一状况,并在每个期间对所有贷款进行所有计算,然后再转到下一个期间。
我在下面附上了当前代码。而不是像现在这样遍历每笔贷款。我需要代码首先遍历每个周期,并在每个周期中进行计算。
我已经尝试通过执行以下操作来反转循环,但我认为我缺少一些东西。问题是,按照原来的方式,我不必每次查看其他贷款的价值。但是新的方法是,对于每笔贷款,我都需要能够引用上期的值,并且我不知道像NotionalEndBal(z,k-1)这样简单的事情是否会起作用?
欢迎任何想法/提示/参考。提前致谢。
' Loop through each loan
For k = 0 To TotalPeriods
' Clear the notional amort arrays
For q = 0 To TotalLoans1
NotionalBegBal(q, 0) = 0
NotionalPMT(q, 0) = 0
NotionalInt(q, 0) = 0
NotionalPrin(q, 0) = 0
NotionalEndBal(q, 0) = 0
NotionalAmortFact(q, 0) = 0
CurRateArray(q, 0) = 0
Next q
' Begin the notional amortization with the zero period balance and the amort factor set to
' one
For z = 1 To TotalLoans1
If k = 0 Then
NotionalEndBal(z, 0) = gp1CurBalArray(z, 0)
NotionalAmortFact(z, 0) = 1
Else
NotionalBegBal(z, 0) = NotionalEndBal(z, k - 1)
' original code below
' Loop through each loan
For k = 1 To TotalLoans1
' Clear the notional amort arrays
For q = 0 To TotalPeriods
NotionalBegBal(q, 0) = 0
NotionalPMT(q, 0) = 0
NotionalInt(q, 0) = 0
NotionalPrin(q, 0) = 0
NotionalEndBal(q, 0) = 0
NotionalAmortFact(q, 0) = 0
CurRateArray(q, 0) = 0
Next q
' Begin the notional amortization with the zero period balance and the amort factor set to
' one
For z = 0 To TotalPeriods
If z = 0 Then
NotionalEndBal(z, 0) = gp1CurBalArray(k, 0)
NotionalAmortFact(z, 0) = 1
Else
NotionalBegBal(z, 0) = NotionalEndBal(z - 1, 0)
' Determine the current period interest rate. If fixed rate use given rate, otherwise
' variable tests need to be run
If FixedFloat = "Fixed" Then
CurRateArray(z, 0) = gp1FxdRateArray(k, 0)
Else
' Floating rate assets in the first period use the introductory interest rate
If z = 1 Then
CurRateArray(z, 0) = gp1IntroRateArray(k, 0)
Else
' Floating rate assets at their first reset are bound by the initial cap
If z = gp1ResetFreqArray(k, 0) Then
CurRateArray(z, 0) = min(gp1AssetFloatArray(z, _
gp1IndexArray(k, 0)) + gp1MarginArray(k, 0), _
CurRateArray(z - 1, 0) + gp1InitCapArray(k, 0), _
gp1CeilingArray(k, 0))
Else
' Otherwise the assets are bound by the subsequent cap and the ceiling
If z Mod gp1ResetFreqArray(k, 0) = 0 Then
CurRateArray(z, 0) = min(gp1AssetFloatArray(z, _
gp1IndexArray(k, 0)) + gp1MarginArray(k, 0), _
CurRateArray(z - 1, 0) + gp1SubCapArray(k, 0), _
gp1CeilingArray(k, 0))
Else
CurRateArray(z, 0) = CurRateArray(z - 1, 0)
End If
End If
End If
End If
' If the payment is already provided then use it, otherwise calculate it based on
' Original Term, Rate, and Balance
If gp1ProvPmtArray(k, 0) > 0 Then
NotionalPMT(z, 0) = min(gp1ProvPmtArray(k, 0), _
NotionalBegBal(z, 0) + (CurRateArray(z, 0) * _
DayFactorArray(z, 0)) * NotionalBegBal(z, 0))
Else
NotionalPMT(z, 0) = min(VBA.Pmt(CurRateArray(z, 0) * _
DayFactorArray(z, 0), gp1OrgTermArray(k, 0), gp1OrgBalArray(k, 0) * -1), _
NotionalBegBal(z, 0) + (CurRateArray(z, 0) * _
DayFactorArray(z, 0)) * NotionalBegBal(z, 0))
End If
' Pay notional interest
NotionalInt(z, 0) = (CurRateArray(z, 0) * DayFactorArray(z, 0)) _
* NotionalBegBal(z, 0)
' Check if loan is in an interest only period, if so do not pay principal. Otherwise
' principal is payment less interest
If z <= gp1IOPdsArray(k, 0) Then
NotionalPrin(z, 0) = 0
Else
NotionalPrin(z, 0) = NotionalPMT(z, 0) - _
NotionalInt(z, 0)
End If
' Calculate ending balance and amortization factor
NotionalEndBal(z, 0) = NotionalBegBal(z, 0) - _
NotionalPrin(z, 0)
NotionalAmortFact(z, 0) = NotionalEndBal(z, 0) / _
NotionalEndBal(0, 0)
End If
Next z
next k
[亚历杭德罗]。>
如果我能理解您,并且您需要的只是反转for循环,请看下面的代码,我认为它可以为您提供帮助。
Sub Macro3()
Dim total As Integer
total = 3
' NORMAL FOR
For i = 1 To total
MsgBox i
Next
' INVERTED FOR
For i = total To 1 Step -1
MsgBox i
Next
End Sub