Syntax fejl i VBA
Hej,Jeg har forsøgt at følge en guide og lave en amortisation skema. Jeg får en fejl jeg ikke kan finde løsningen på.
Sub Loan_Amort_V1()
Dim intRate, initLoanAmnt, loanLife
Dim yrBegBal, yrEndBal
Dim annualPmnt, intComp, princRepay
Dim outRow, rowNum, outSheet
'************************************************************
' Programmer inputs
'************************************************************
outRow = 5 'Used to control where the output table will start
outSheet = "Loan Amort"
Worksheets(outSheet).Activate
'Clear previous data
Rows(outRow + 4 & ":" & outRow + 300).Select
Selection.Clear
Range("B2:B4").Select
Selection.ClearContents
'************************************************************
'Get user inputs
'************************************************************
' The user provides these input data through dialog boxes.
' Input data not meeting specified criteria are not accepted
Do
intRate = InputBox("Enter interest rate in percent" _
& " without % sign. It must be between 0% and 15%")
If intRate < 0 Or intRate > 15 Then
MsgBox ("Int. rate must be between 0% and 15%.")
Else
Exit Do
End If
Loop
intRate = intRate / 100
Do
loanLife = InputBox("Enter loan life in years." _
& " Loan life must be a whole number.")
If loanLife < 0 Or (loanLife - Round(loanLife) <> 0) Then
MsgBox ("Loan life must be a whole number.")
Else
Exit Do
End If
Loop
GetLoanAmnt:
initLoanAmnt = InputBox("Enter loan amount." _
& " Loan amount must be a positive whole number.")
If initLoanAmnt < 0 Or (initLoanAmnt - Round(initLoanAmnt) _
<> 0) Then
MsgBox ("Loan amount must be a positive whole number.")
GoTo GetLoanAmnt
End If
'************************************************************
' Write out the input data on the output sheet
'************************************************************
Cells(2, 2).Value = intRate
Cells(3, 2).Value = loanLife
Cells(4, 2).Value = initLoanAmnt
'************************************************************
' Compute and output results
'************************************************************
' Calculate annual payment
annualPmnt = Pmt(intRate, loanLife, -initLoanAmnt, , 0)
' Initialize beginning balance for year 1
yrBegBal = initLoanAmnt
' Loop to calculate and output year-by-year amort. table
For rowNum = 1 To loanLife
intComp = yrBegBal * intRate
princRepay = annualPmnt - intComp
yrEndBal = yrBegBal - princRepay
Cells(outRow + rowNum + 3, 3).Value = rowNum 'Year number
Cells(outRow + rowNum + 3, 4).Value = yrBegBal
Cells(outRow + rowNum + 3, 5).Value = annualPmnt
Cells(outRow + rowNum + 3, 6).Value = intComp
Cells(outRow + rowNum + 3, 7).Value = princRepay
Cells(outRow + rowNum + 3, 8).Value = yrEndBal
yrBegBal = yrEndBal
Next rowNum
'************************************************************
' Format the output data in the table
'************************************************************
Range(Cells(outRow + 4, 4), Cells(outRow + loanLife + 3, 8) _
).Select
Selection.NumberFormat = "$#,##0"
End Sub
Fejlen er: Run-time Error "9":
Subscript out of range.
Håber nogen kan give et hint.
//Mads...