macros来检查行范围值和更改另一个值
我是完全新的Excel VBA,
我一直在做一个贷款摊销项目,以帮助我和我的朋友的任何贷款。
问题是贷款摊还一段时间,比如有239条支付线的20年
通过一个if公式自动生成的最后一行需要一些Reconciliations,见下文
只要最后的计划付款less于6309的实际付款,则计划的付款应该等于2711 + 4
我可以创build任何macros来做这个逻辑:(我不是一个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
根据您的例子假设,最后一行将始终包含您的上次付款,您可以快速检查这些值并填写最终付款金额:
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
否则,并不是很明显,为什么你需要扫描分期付款表中的所有行才能find最后一行。
或者,你可以在你的例子中使用单元格G5
中的公式:
=IF(C5<D4,C5+F5,D4)