[英]How to check cell value to change value of another?
我正在做一個貸款攤銷項目。
貸款在一段時間內攤銷。 假設 20 年有 239 條支付線。
只要最后一次預定付款小於實際付款 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
根據您的示例,假設最后一行將始終包含您的最后一筆付款,您只需快速檢查這些值並填寫最終付款金額:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.