Vba 使用Excel宏的多行摊销计划

Vba 使用Excel宏的多行摊销计划,vba,excel,Vba,Excel,我是Excel VBA新手,正在寻求有关编辑宏的帮助 我有三列7328行的批准金额、使用期限和利率 我已经有了一个工作的VBA脚本(如下所示)来计算摊销计划,但我希望它从3列中获取输入,计算7328行(而不是手动输入),并将值附加到彼此下面 我请求更改下面的脚本以获取3列中的值 Sub one() Dim intRate, loanLife, initLoan, payment As Double Dim yearBegBal, intComp, prinComp, yearEndBal,

我是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

添加数据样本。@SivapasathVadivel我已在图像中添加了数据样本。您有值的输入框……您只需在行中循环,从单元格值填充变量,即可获得计算结果。请为您提供的示例数据添加所需的输出。对于reference@SivaprasathVadivel我不知道如何使用循环将文本框替换为输入值这就是我寻求帮助的原因。。我已经添加了来自我的脚本的结果。我尝试只针对范围B2:B7运行脚本,但如果您在同一工作簿中运行代码,它仍然会抛出一个错误“下标超出范围”…请尝试thisworkbook.sheets(1).Range(“B2:B7328”)。使用thisworkbook.sheets(1).Range(“B2:B2001”)运行它仍然会给出相同的错误“下标超出范围”您的工作簿是否有表1以及B、C、D列是否存在?是的,它有B、C、D列。实际上,在运行代码之前,我需要创建表(2)。我这样做了,它运行了。非常感谢。我将粘贴完整的代码,以防将来其他人需要它
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