在列中查找具有值“0”的行,然后在具有上述行的SUM行中查找

我试图使一个ExcelmacrossearchSheets("Data").Range("B:" & lastRow)的值为“0”。

如果在列BI中发现值为“0”的行,则需要对该行进行SUM和合并。

我有下面的代码,但我得到错误,我不知道我在做什么错:

 Sub sumZeroRows() Dim ws As Worksheet Dim lastrow As Long Dim i As Long Set ws = Sheets("Data") With ws lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row For i = lastrow To 11 Step -1 If .Cells(i, "B").Value = "0" Then .Cells(i - 1, "C").Value = .Cells(i - 1, "C").Value2 + .Cells(i, "C").Value2 .Cells(i - 1, "D").Value = .Cells(i - 1, "D").Value2 + .Cells(i, "D").Value2 .Cells(i - 1, "E").Value = .Cells(i - 1, "E").Value2 + .Cells(i, "E").Value2 .Cells(i - 1, "F").Value = .Cells(i - 1, "F").Value2 + .Cells(i, "F").Value2 .Cells(i - 1, "G").Value = .Cells(i - 1, "G").Value2 + .Cells(i, "G").Value2 .Cells(i - 1, "H").Value = .Cells(i - 1, "H").Value2 + .Cells(i, "H").Value2 .Cells(i - 1, "I").Value = .Cells(i - 1, "I").Value2 + .Cells(i, "I").Value2 .Cells(i - 1, "J").Value = .Cells(i - 1, "J").Value2 + .Cells(i, "J").Value2 .Cells(i - 1, "K").Value = .Cells(i - 1, "K").Value2 + .Cells(i, "K").Value2 .Cells(i - 1, "L").Value = .Cells(i - 1, "L").Value2 + .Cells(i, "L").Value2 .Cells(i - 1, "M").Value = .Cells(i - 1, "M").Value2 + .Cells(i, "M").Value2 .Cells(i - 1, "N").Value = .Cells(i - 1, "N").Value2 + .Cells(i, "N").Value2 .Cells(i - 1, "O").Value = .Cells(i - 1, "O").Value2 + .Cells(i, "O").Value2 .Cells(i - 1, "P").Value = .Cells(i - 1, "P").Value2 + .Cells(i, "P").Value2 .Cells(i - 1, "Q").Value = .Cells(i - 1, "Q").Value2 + .Cells(i, "Q").Value2 .Cells(i - 1, "R").Value = .Cells(i - 1, "R").Value2 + .Cells(i, "R").Value2 .Rows(i).Delete End If Next i End With End Sub 

我的电池看起来像 这个

这就是你想要的:

 Sub sumZeroRows() Dim ws As Worksheet Dim lastrow As Long Dim i As Long Set ws = Sheets("Data") With ws lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row For i = lastrow To 11 Step -1 If .Cells(i, "B").Value = "0" Then .Cells(i - 1, "C").Value = .Cells(i - 1, "C").Value2 + .Cells(i, "C").Value2 .Cells(i - 1, "D").Value = .Cells(i - 1, "D").Value2 + .Cells(i, "D").Value2 .Rows(i).Delete End If Next i End With End Sub 

删除行时,您需要向后循环。 并逐一求和每列。