我的编码有溢出错误

时间:2019-04-08 09:18:22

标签: excel vba loops iteration

我需要为固定贷款制作一个摊销表,该表以等额的年度分期付款方式支付,第一笔应在第一年末支付。我使用的是可变利率和期限,但是我的代码有些不正确,我不确定是什么。

我尝试进入,但是它突出了很多内容,我不确定出什么问题。

这是我的代码:

Sub Loan_Amort_VBR()

' This version modifies the fixed rate iteration model to accommodate 3 different
' interest rates specified by the user.

Dim initLoanAmount, LoanLife
Dim yrBegBal(100), YrEndBal(), finalBal
Dim ipPay(100, 2), intRate(1 To 100)
Dim per1, per2, per3, int1, int2, int3
Dim annPayment, annPmtOld
Dim OutputRow, Rowno, NoOfIter, BalTol
Dim OutputSheet, iCol, pCol

'************************************************************
' User inputs
'************************************************************
' Read in from data entered by user on worksheet
initLoanAmount = Cells(2, 2).Value 'initial loan balance
per1 = Cells(3, 2).Value 'Length of period 1
per2 = Cells(4, 2).Value
per3 = Cells(5, 2).Value
int1 = Cells(6, 2).Value 'Interest rate for period 1
int2 = Cells(7, 2).Value
int3 = Cells(8, 2).Value

LoanLife = per1 + per2 + per3
'************************************************************
' Programmer inputs
'************************************************************
OutputSheet = "Loan Amort VBR"
BalTol = 1 'Specifies desired accuracy
iCol = 1
pCol = 2
OutputRow = 12 'row below which repayment schedule would start

'************************************************************
' Preliminaries
'************************************************************
' Make the outSheet the active sheet
Worksheets(OutputSheet).Activate

' Clear previous data
Rows(OutputRow + 1 & ":" & OutputRow + 300).Select
Selection.Clear

ReDim YrEndBal(LoanLife) 'Redimension the array

'************************************************************
' Computations and output
'************************************************************
' Enter appropriate interest rates into the intRate array
For Rowno = 1 To LoanLife
If Rowno <= per1 Then
    intRate(Rowno) = int1
ElseIf Rowno <= per1 + per2 Then
    intRate(Rowno) = int2
Else
    intRate(rNum) = int3
End If
Next

annPayment = initLoanAmount * Application.Min(int1, int2, int3)
NoOfIter = 0 ' Counter for number of iterations

' This Do loop controls the iteration
Do While finalBal > BalTol Or finalBal = 0

    ' Initialize balance at the beginning of year 1
    yrBegBal(1) = initLoanAmount

    ' Loop to calculate and store year-by-year data
    For Rowno = 1 To LoanLife
        ipPay(Rowno, iCol) = yrBegBal(Rowno) * intRate(Rowno)
        ipPay(Rowno, pCol) = annPayment - ipPay(Rowno, iCol)
        YrEndBal(Rowno) = yrBegBal(Rowno) - ipPay(Rowno, pCol)

        yrBegBal(Rowno + 1) = YrEndBal(Rowno)
    Next Rowno

    finalBal = YrEndBal(LoanLife)
    annPmtOld = annPayment

    ' Calculate the next annual payment to try
    annPayment = annPayment + (finalBal * (1 + _
      Application.Max(int1, int2, int3)) ^ _
      (-LoanLife)) / LoanLife
    NoOfIter = NoOfIter + 1 ' Count iterations

Loop

'***************************************************************
' Output data to worksheet
'***************************************************************
For Rowno = 1 To LoanLife
    Cells(OutputRow + Rowno, 3).Value = Rowno 'Year number
    Cells(OutputRow + Rowno, 4).Value = yrBegBal(Rowno)
    Cells(OutputRow + Rowno, 5).Value = annPmtOld
    Cells(OutputRow + Rowno, 6).Value = ipPay(Rowno, iCol)
    Cells(OutputRow + Rowno, 7).Value = ipPay(Rowno, pCol)
    Cells(OutputRow + Rowno, 8).Value = YrEndBal(Rowno)
    Cells(OutputRow + Rowno, 9).Value = intRate(Rowno)
Next Rowno

' Print out number of iterations used
Cells(OutputRow + LoanLife + 4, 1).Value = "No. of iterations ="
Cells(OutputRow + LoanLife + 4, 2).Value = numOfIterations

'************************************************************
' Format data in table
'************************************************************
Range(Cells(OutputRow + 1, 4), Cells(OutputRow + LoanLife, 8)). _
  Select
Selection.NumberFormat = "$#,##0"

Range(Cells(OutputRow + 1, 9), Cells(OutputRow + LoanLife, 9)). _
  Select
Selection.NumberFormat = "0.0#%"

End Sub

当我进入代码时,这些部分以黄色突出显示:

Sub Loan_Amort_VBR()
initLoanAmount = Cells(2, 2).Value 'initial loan balance
per1 = Cells(3, 2).Value 'Length of period 1
per2 = Cells(4, 2).Value
per3 = Cells(5, 2).Value
int1 = Cells(6, 2).Value 'Interest rate for period 1
int2 = Cells(7, 2).Value
int3 = Cells(8, 2).Value
LoanLife = per1 + per2 + per3
OutputSheet = "Loan Amort VBR"
BalTol = 1 'Specifies desired accuracy
iCol = 1
pCol = 2
OutputRow = 12 'row below which repayment schedule would start
Worksheets(OutputSheet).Activate

0 个答案:

没有答案