代码之家  ›  专栏  ›  技术社区  ›  Salem Mohd

匹配总金额为零的引用

  •  -4
  • Salem Mohd  · 技术社区  · 6 年前

    enter image description here

    亲爱的朋友们

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

    在上述情况下,系统应检查第#1行,并通过将A1与A2字段匹配,将其与第#2行匹配,将B1与B2匹配,并且C1和C2的总和=应为零。

    一旦匹配,系统应删除这两行,并再次重复此过程直至工作表结束。

    谢谢

    塞勒姆H。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Xabier    6 年前

    以下内容如何:

    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