[英]Search for selected cell in a list and change cells value next to it
我正面临这个问题,我不知道是否可以在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的-200更改为200,将158的更改为-300至300,依此类推
所以我的想法是:选择贷款编号,然后单击一个按钮,这样按钮事件将在我的贷款列表中搜索选定的值,找到它,然后将下一个负数更改为正
任何帮助将不胜感激!
注意我在图纸和范围位置上所做的假设,并根据需要进行调整。 这是一个很棒的通用外壳,但是您未提供的数据中可能存在细微差别,可能需要进行代码调整。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.