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

时间:2016-10-10 19:14:44

标签: excel vba excel-vba

我正面临这个问题,我无法弄清楚是否可以在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等等

所以我想到的是: 选择贷款编号,然后单击一个按钮,这样按钮事件将在我的贷款清单中搜索所选值,找到它然后将下一个负数更改为正面

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

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

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