VBA摊销表开发求助:For Next循环迭代次数及内容显示异常
问题描述
我正在修读应用/高级财务分析课程,本周作业要求使用3个(或4个)InputBox输入数据,在MsgBox中显示摊销计划表。
我已经联系过教授,但自身知识储备不足,无法理解指导内容。
我能调出所有4个输入框以及消息框,但循环内容无法显示。我尝试将循环计算结果存储到变量中,但毫无头绪。
原VBA代码
Sub PaymentScheduleCalculator() Dim PV As Single '10000 Dim years As Single '2 Dim frequency As Double '12 Dim rate As Variant '4% APR Dim Ppmt As Double Dim Ipmt As Double Dim Pmt As Single 'for pmt after each year Dim i As Integer 'designation for loop Dim Temp As Integer Dim TempVars! For i = 1 To n * frequency Pmt = PV * rate / frequency TempVars! = Temp & vbNewLine & i & _ vbTab & FormatCurrency(PV, 2) & _ vbTab & FormatCurrency(Pmt, 2) & _ vbTab & FormatCurrency(Ipmt, 2) & _ vbTab & FormatCurrency(-Ipmt, 2) PV = PV - Pmt + Ipmt Next i PV = InputBox("How much money do you want to borrow?", "Payment Calculator", 10000) years = InputBox("If you borrow " & FormatCurrency(PV) & " - how many years do want to borrow the money for?", "Payment Calculator", 2) rate = InputBox("If you borrow " & FormatCurrency(PV) & " for " & years & " years, " & "what interest rate are you paying?", "Payment Calculator", 0.04) If Right(rate, 1) = "%" Then rate = Val(Left(rate, Len(rate) - 1) / 100) Else rate = rate End If frequency = InputBox("If you borrow " & FormatCurrency(PV) & " at " & FormatPercent(rate) & "," & " for " & years & " years, " & _ "how many payment intervals are there per year?", "Payment Calculator", 12) 'runs fine until here but does not display the loop MsgBox "Loan Amount " & FormatCurrency(PV) & _ vbNewLine & "Number of Payments " & years * frequency & _ vbNewLine & "Interest Rate " & FormatPercent(rate) & _ vbNewLine & _ vbNewLine & "PMT # " & vbTab & "Balance " & vbTab & "Payment " & vbTab & "Interest " & vbTab & "Capital " & _ vbNewLine & RepeatCalc, , "Payment Calculator" End Sub
问题修正要点
- 执行顺序错误:循环写在输入数据之前,此时所有变量未赋值,且循环中使用了未定义的
n(应为years),导致循环根本无法正确执行。 - 变量类型错误:
Temp和TempVars!被定义为数值类型,无法存储拼接的文本内容,应改为String类型;MsgBox中使用的RepeatCalc变量从未定义和赋值,所以无法显示循环内容。 - 摊销计算逻辑错误:原代码的月供计算不符合等额本息规则,且未正确计算每期利息(Ipmt)和本金偿还额(Ppmt),同时直接修改原始PV值会导致后续数据错误,需用临时变量跟踪剩余本金。
修正后的代码
Sub PaymentScheduleCalculator() Dim originalPV As Single ' 原始贷款金额,避免被循环修改 Dim years As Single Dim frequency As Double Dim rate As Variant Dim monthlyRate As Double ' 每期利率 Dim totalPayments As Integer ' 总还款期数 Dim Pmt As Double ' 每期还款额 Dim remainingPV As Double ' 剩余本金 Dim Ipmt As Double ' 当期利息 Dim Ppmt As Double ' 当期偿还本金 Dim i As Integer Dim scheduleText As String ' 存储摊销表文本 ' 输入数据 originalPV = InputBox("How much money do you want to borrow?", "Payment Calculator", 10000) years = InputBox("If you borrow " & FormatCurrency(originalPV) & " - how many years do want to borrow the money for?", "Payment Calculator", 2) rate = InputBox("If you borrow " & FormatCurrency(originalPV) & " for " & years & " years, what interest rate are you paying?", "Payment Calculator", 0.04) If Right(rate, 1) = "%" Then rate = Val(Left(rate, Len(rate) - 1)) / 100 End If frequency = InputBox("If you borrow " & FormatCurrency(originalPV) & " at " & FormatPercent(rate) & " for " & years & " years, how many payment intervals are there per year?", "Payment Calculator", 12) ' 计算基础参数 monthlyRate = rate / frequency totalPayments = years * frequency ' 使用PMT函数计算每期等额还款额,负数表示支出 Pmt = Abs(WorksheetFunction.Pmt(monthlyRate, totalPayments, -originalPV)) remainingPV = originalPV ' 构建摊销表文本 scheduleText = "PMT # " & vbTab & "Balance " & vbTab & "Payment " & vbTab & "Interest " & vbTab & "Capital " & vbNewLine For i = 1 To totalPayments ' 计算当期利息 Ipmt = remainingPV * monthlyRate ' 计算当期偿还本金 Ppmt = Pmt - Ipmt ' 更新剩余本金 remainingPV = remainingPV - Ppmt ' 拼接当期数据到文本 scheduleText = scheduleText & i & _ vbTab & FormatCurrency(remainingPV, 2) & _ vbTab & FormatCurrency(Pmt, 2) & _ vbTab & FormatCurrency(Ipmt, 2) & _ vbTab & FormatCurrency(Ppmt, 2) & vbNewLine Next i ' 显示结果 MsgBox "Loan Amount " & FormatCurrency(originalPV) & _ vbNewLine & "Number of Payments " & totalPayments & _ vbNewLine & "Interest Rate " & FormatPercent(rate) & _ vbNewLine & vbNewLine & scheduleText, vbOKOnly, "Payment Calculator" End Sub
内容的提问来源于stack exchange,提问作者Excelnoob
相关产品推荐
相关产品推荐

