[英]Amortization schedule for many rows using Excel Macro
我是Excel VBA的新手,正在寻找有关编辑宏的帮助。
我有7328行的“批准金额”,“任期”和“利率”三列。
我已经有一个可以使用的VBA脚本(如下所示)来计算摊销时间表,但我希望它从3列中获取输入,计算7328行(而不是手动将其输入),并将值附加在彼此之间。
我要求更改以下脚本以从3列中获取值。
Sub one()
Dim intRate, loanLife, initLoan, payment As Double
Dim yearBegBal, intComp, prinComp, yearEndBal, intTot, prinTot, fvloan As Currency
ActiveSheet.UsedRange.Delete
intRateYrs = InputBox("Input Interest rate (Annual):")
loanLifeYrs = InputBox("Input Loan life (Years):")
initLoan = InputBox("Input Loan amount:")
Application.DisplayAlerts = True
Application.ScreenUpdating = True
intRateMths = (intRateYrs / 100) / 12
loanLifeMths = loanLifeYrs * 12
Cells(4, 2).Value = Format(intRateYrs, "#.##") & " %"
Cells(4, 3).Value = Format(intRateMths, "Percent")
Cells(5, 2).Value = loanLifeYrs
Cells(5, 3).Value = loanLifeMths
Cells(6, 2).Value = Format(initLoan, "Currency")
payment = Pmt(intRateMths, loanLifeMths, -initLoan)
Cells(7, 2).Value = Format(payment, "Currency")
outRow = 10
intTot = 0
prinTot = 0
fvloan = 0
Cells(10, 2).Value = "Beginning Balance"
Cells(10, 3).Value = "Payment"
Cells(10, 4).Value = "Interest"
Cells(10, 5).Value = "Principal"
Cells(10, 6).Value = "End Balance"
Cells(10, 7).Value = "Total Interest"
Cells(10, 8).Value = "Total Principal"
Cells(10, 9).Value = "Total Repaid"
yearBegBal = initLoan
For rowNum = 1 To loanLifeMths
intComp = yearBegBal * intRateMths
prinComp = payment - intComp
yearEndBal = yearBegBal - prinComp
intTot = intTot + intComp
prinTot = prinTot + prinComp
fvloan = intTot + prinTot
Cells(outRow + rowNum, 1).Value = rowNum
Cells(outRow + rowNum, 2).Value = Format(yearBegBal, "Currency")
Cells(outRow + rowNum, 3).Value = Format(payment, "Currency")
Cells(outRow + rowNum, 4).Value = Format(intComp, "Currency")
Cells(outRow + rowNum, 5).Value = Format(prinComp, "Currency")
Cells(outRow + rowNum, 6).Value = Format(yearEndBal, "Currency")
Cells(outRow + rowNum, 7).Value = Format(intTot, "Currency")
Cells(outRow + rowNum, 8).Value = Format(prinTot, "Currency")
Cells(outRow + rowNum, 9).Value = Format(fvloan, "Currency")
yearBegBal = yearEndBal
Next rowNum
ActiveSheet.Range("A:I").EntireColumn.AutoFit
Rows("11:11").Select
ActiveWindow.FreezePanes = True
Range("A1").Select
Application.DisplayAlerts = False
Application.ScreenUpdating = False
End Sub
Sub one()
Dim intRate, loanLife, initLoan, payment As Double
Dim yearBegBal, intComp, prinComp, yearEndBal, intTot, prinTot, fvloan As Currency
Dim cell As Range
For Each cell In ThisWorkbook.Sheets(1).Range("B2:B2001")
'intRateYrs = InputBox("Input Interest rate (Annual):")
'loanLifeYrs = InputBox("Input Loan life (Years):")
'initLoan = InputBox("Input Loan amount:")
intRateYrs = cell.Offset(0, 1).Value
loanLifeYrs = cell.Value
initLoan = cell.Offset(0, 2).Value
lrow = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row
Application.DisplayAlerts = True
Application.ScreenUpdating = True
intRateMths = (intRateYrs / 100) / 12
loanLifeMths = loanLifeYrs * 12
With Sheets(2)
Cells(lrow + 4, 2).Value = Format(intRateYrs, "#.##") & " %"
Cells(lrow + 4, 3).Value = Format(intRateMths, "Percent")
Cells(lrow + 5, 2).Value = loanLifeYrs
Cells(lrow + 5, 3).Value = loanLifeMths
Cells(lrow + 6, 2).Value = Format(initLoan, "Currency")
payment = Pmt(intRateMths, loanLifeMths, -initLoan)
Cells(lrow + 7, 2).Value = Format(payment, "Currency")
outRow = lrow + 10
intTot = 0
prinTot = 0
fvloan = 0
Cells(lrow + 10, 2).Value = "Beginning Balance"
Cells(lrow + 10, 3).Value = "Payment"
Cells(lrow + 10, 4).Value = "Interest"
Cells(lrow + 10, 5).Value = "Principal"
Cells(lrow + 10, 6).Value = "End Balance"
Cells(lrow + 10, 7).Value = "Total Interest"
Cells(lrow + 10, 8).Value = "Total Principal"
Cells(lrow + 10, 9).Value = "Total Repaid"
yearBegBal = initLoan
For rowNum = 1 To loanLifeMths
intComp = yearBegBal * intRateMths
prinComp = payment - intComp
yearEndBal = yearBegBal - prinComp
intTot = intTot + intComp
prinTot = prinTot + prinComp
fvloan = intTot + prinTot
Cells(outRow + rowNum, 1).Value = rowNum
Cells(outRow + rowNum, 2).Value = Format(yearBegBal, "Currency")
Cells(outRow + rowNum, 3).Value = Format(payment, "Currency")
Cells(outRow + rowNum, 4).Value = Format(intComp, "Currency")
Cells(outRow + rowNum, 5).Value = Format(prinComp, "Currency")
Cells(outRow + rowNum, 6).Value = Format(yearEndBal, "Currency")
Cells(outRow + rowNum, 7).Value = Format(intTot, "Currency")
Cells(outRow + rowNum, 8).Value = Format(prinTot, "Currency")
Cells(outRow + rowNum, 9).Value = Format(fvloan, "Currency")
yearBegBal = yearEndBal
Next rowNum
ActiveSheet.Range("A:I").EntireColumn.AutoFit
Rows("11:11").Select
ActiveWindow.FreezePanes = True
Range("A1").Select
End With
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Next cell
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.