将参考文献与零金额相匹配

时间:2018-01-29 12:04:20

标签: excel vba match

enter image description here

亲爱的朋友们

我正在尝试编写一个VBA代码,可以删除匹配的行,代码应该在行中匹配(前A1匹配A2,B1匹配B2)和C1和& C2 =零。

在上述情况下,系统应检查第1行并将其与第2行匹配,方法是将A1与A2字段匹配,并将B1与B2匹配,C1和C2的总和也应为nill。

一旦匹配的系统应删除这两行,并再次将此过程重复到工作表的末尾。

谢谢

Salem H。

1 个答案:

答案 0 :(得分:0)

以下内容如何:

Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
For i = 1 To LastRow
    aValue = ws.Cells(i, 1) 'get values from first row
    bValue = ws.Cells(i, 2)
    cValue = ws.Cells(i, 3)
    If aValue = ws.Cells(i + 1, 1) And bValue = ws.Cells(i + 1, 2) Then 'compare to next row
        If cValue + ws.Cells(i + 1, 3) = O Then 'if adding values from Column C = 0 then
            ws.Cells(i, 3) = 0 'write 0 in both rows
            ws.Cells(i + 1, 3) = 0
        End If
    End If
Next i
End Sub