在列表中search选定的单元格,并更改其旁边的单元格值

我面临这个问题,我不知道是否可以在VBA中完成

我有一个贷款清单,每个受益人连续存储如下

loan# loan jan2016 feb2016 mar2016 apr2016 156 1000 -250 -250 -250 -250 157 800 -200 -200 -200 -200 158 1200 -300 -300 -300 -300 

我收到一份清单,列出支付款项的人:

 157 200 158 300 

所以我手工做的是把贷款157和-300改为-200至200为158等等

所以我想到的是:select贷款编号,然后点击一个button,所以button事件将在我的贷款清单中search选定的值,find它,然后将下一个负数改为正

任何帮助将不胜感激!

注意我在表单和范围位置上做出的假设,并根据需要进行调整。 这是一个很好的通用shell,但是您的数据中可能存在细微的差别,您可能需要对代码进行调整。

 Option Explicit Sub ApplyPayments() Dim wsPay as Worksheet, wsLoans as Worksheet Set wsPay = Worksheets("Payments") Set wsLoans = Worksheets("Loans") With wsPay Dim rPayment as range For each rPayment in .Range("A2:A100") 'assumes payments listed in this range 'with loan number in col A and payment in B With wsLoans Dim rLoan as Range Set rLoan = .Columns(1).Find(rPayment.Value, lookat:=xlWhole) 'above assumes loans listed in column A of loan sheet. If Not rLoan is Nothing Then Dim rPayApply as Range Set rPayApply = rLoan.EntireRow.Find(rPayment.Offset(,1) * -1, lookat:=xlWhole) 'again, assumes payment amount in col B If not rPayApply is Nothing Then rPayApply.Value = rPayApply.Value * -1 End If End With Next End With End Sub