我在这里看到如何通过这个网站分摊一笔贷款,我能够使用下面的代码让它工作。但我现在正在尝试同时摊还多笔贷款,并在最后生成汇总信息。我在网上找不到关于如何做到这一点的任何想法,关于从哪里开始的任何想法?
先谢谢
Sub LoanAmort()
'take user inputs from top of the sheet Rate, Term, Orignal
'declares these as variables
Dim Rate, Term, Orignal, Payment
'telling VBA where these variables are
Rate = Sheets("Sheet1").Range("B1").Value
Term = Sheets("Sheet1").Range("B2").Value
OrigBal = Sheets("Sheet1").Range("B3").Value
'Calc PMT
Payment = PMT((Rate / 12), Term, -OrigBal)
'Declare the outputs
Dim BeginBal, Interest, Principal, EndBal
'Declare output Rows
OutputRow = 7
'Delete all previous information
Range(Cells(OutputRow + 1, 1), Cells(10000, 6)).Select
Selection.ClearContents
'Fill in rows
BeginBal = OrigBal
For rowNum = 1 To Term
Interest = BeginBal * (Rate / 12)
Principal = Payment - Interest
EndBal = BeginBal - Principal
'populate the cells
Cells(OutputRow + rowNum, 1).Value = rowNum
Cells(OutputRow + rowNum, 2).Value = BeginBal
Cells(OutputRow + rowNum, 3).Value = Payment
Cells(OutputRow + rowNum, 4).Value = Interest
Cells(OutputRow + rowNum, 5).Value = Principal
Cells(OutputRow + rowNum, 6).Value = EndBal
BeginBal = EndBal
Next rowNum
'Formatting
Range(Cells(OutputRow + 1, 2), Cells(OutputRow + Term, 6)).Select
Selection.NumberFormat = "$#,###.#0"
End Sub
答案 0 :(得分:0)
假设您的多笔贷款的信息位于B,C,D等列的第1行到第3行,那么您可能只想循环遍历这些列:
Option Explicit
Sub LoanAmort()
Dim Rate As Double, Term As Long, OrigBal As Double, Payment As Double
Dim lastCol As Long
Dim colNum As Long
Dim rowNum As Long
Dim OutputRow As Long
Dim BeginBal As Double, Interest As Double, Principal As Double, EndBal As Double
'Declare output Rows
OutputRow = 7
With Worksheets("Sheet1") ' I'm lazy and don't want to keep typing Worksheets("Sheet1")
'Delete all previous information
.Range(.Cells(OutputRow + 1, 1), .Cells(10000, 6)).ClearContents
'Find last column
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
'Loop through each column
For colNum = 2 To lastCol
'telling VBA where these variables are
Rate = .Cells(1, colNum).Value
Term = .Cells(2, colNum).Value
OrigBal = .Cells(3, colNum).Value
'Calc PMT
Payment = Pmt((Rate / 12), Term, -OrigBal)
'Fill in rows
BeginBal = OrigBal
For rowNum = 1 To Term
Interest = BeginBal * (Rate / 12)
Principal = Payment - Interest
EndBal = BeginBal - Principal
'populate the cells
.Cells(OutputRow + rowNum, 1).Value = rowNum
.Cells(OutputRow + rowNum, 2).Value = .Cells(OutputRow + rowNum, 2).Value + BeginBal
.Cells(OutputRow + rowNum, 3).Value = .Cells(OutputRow + rowNum, 3).Value + Payment
.Cells(OutputRow + rowNum, 4).Value = .Cells(OutputRow + rowNum, 4).Value + Interest
.Cells(OutputRow + rowNum, 5).Value = .Cells(OutputRow + rowNum, 5).Value + Principal
.Cells(OutputRow + rowNum, 6).Value = .Cells(OutputRow + rowNum, 6).Value + EndBal
BeginBal = EndBal
Next
Next
'Formatting
.Range(.Cells(OutputRow + 1, 2), .Cells(OutputRow + Term, 6)).NumberFormat = "$#,###.#0"
End With
End Sub