【问题标题】:How to check cell value to change value of another?如何检查单元格值以更改另一个值?
【发布时间】:2016-02-24 07:11:45
【问题描述】:

我正在做一个贷款摊销项目。

贷款在一段时间内摊销。假设 20 年有 239 条支付线。

通过 if 公式自动生成的最后一行需要一些对账

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

我一直在尝试编辑这段代码。

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

【问题讨论】:

    标签: vba excel amortization


    【解决方案1】:

    根据您的示例,假设最后一行将始终包含您的最后一次付款,您只需快速检查值并填写最终付款金额:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 2020-02-17
      相关资源
      最近更新 更多