宏来检查行范围值并更改另一个值

时间:2016-02-24 07:11:45

标签: excel vba excel-vba amortization

我是Excel VBA的新手,

我一直致力于贷款摊还项目,以帮助我和我的朋友获得任何贷款。

问题是贷款是否在一段时间内摊销让我们说20年有239条支付线

通过if公式自动生成的最后一行需要一些Reconciliations,见下文

enter image description here

只要上次预定付款小于实际付款6309,那么预定付款应该等于2711 + 4

enter image description here

我可以创建任何宏来做这个逻辑:(我不是VBA专家

我一直试图编辑这段代码而没有运气,这是我最接近的,请你帮忙

Private Sub CommandButton23_Click()

    Dim r1, r2, n As Long
    Dim Pay_Num As Integer, result As String
    Pay_Num = Range("D34").Value

    With Sheets("LOANQUIC & Schedule Table") '~~> change to suit
        Dim lrow As Long
        Number_of_Payments = Range("G20").Value
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row
        r1 = Application.Transpose(.Range("A2:A" & lrow))
        r2 = Application.Transpose(.Range("J2:J" & lrow))
        For n = LBound(r1) To UBound(r1)
            If r1(n) <> "" Then r2(n) = r1(n)
            If r1(n) = Number_of_Payments Then Sched_Pay = Number_of_Payments
            Range("D35").Value = Sched_Pay
        Next
        .Range("J2:J" & lrow) = Application.Transpose(r2)
    End With

End Sub

1 个答案:

答案 0 :(得分:0)

根据您的示例,假设最后一行将始终包含您的上一笔付款,您可以快速检查价值并填写最终付款金额:

Option Explicit

Sub Example1()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim begBalColumn As Long
    Dim schedPayColumn As Long
    Dim interestColumn As Long
    Dim endBalColumn As Long

    '--- you can automatically set these values if needed
    begBalColumn = 3
    schedPayColumn = 4
    interestColumn = 6
    endBalColumn = 7

    '--- assumes the last row has your last payment
    Set ws = ActiveSheet
    lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

    With ws
        If .Cells(lastRow, begBalColumn).Value < _
               .Cells(lastRow - 1, schedPayColumn).Value Then
            .Cells(lastRow, schedPayColumn).Value = .Cells(lastRow, begBalColumn).Value + _
                                                    .Cells(lastRow, interestColumn).Value
        End If
    End With
End Sub

否则,为什么您需要扫描摊销表中的所有行以查找最后一行,这一点并不明显。

或者,您也可以在示例中使用单元格G5中的公式:

=IF(C5<D4,C5+F5,D4)