Avatar billede madsing Nybegynder
05. januar 2014 - 15:15 Der er 2 kommentarer og
1 løsning

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...
Avatar billede supertekst Ekspert
05. januar 2014 - 15:33 #1
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"

outSheet = "Ark1" ' ret arknavn eller omdøb
Avatar billede madsing Nybegynder
06. januar 2014 - 00:22 #2
Tak!!!

Det løste problemet...
Avatar billede supertekst Ekspert
06. januar 2014 - 08:48 #3
Selv tak - du får et svar
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester