这就是我的数据的样子。它是另一张纸的摘要。
代码似乎可以运行并执行我需要的操作,如果有 0 值,则扫描 Co B 并删除,如果下面的行不同,则扫描 Col I 并插入行,然后对 Col H 重复。但是我得到一个“运行时 1004 应用程序-定义或对象定义的错误”消息,而不是刚刚结束的宏。接受任何编辑或建议
Range("A100000").End(xlUp).Activate
Range("N1") = ActiveCell.Row
For lRow = Cells(Cells.Rows.Count, "b").End(xlUp).Row To 1 Step -1
If Cells(lRow, "b") = 0 Then
Rows(lRow).EntireRow.Delete
End If
Next lRow
For lRow = Cells(Cells.Rows.Count, "I").End(xlUp).Row To 1 Step -1
If Cells(lRow, "I") <> Cells(lRow - 1, "I") Then '<~~ debugger highlights this line or the other version of this eq below
Rows(lRow).EntireRow.Insert
End If
Next lRow
For lRow = Cells(Cells.Rows.Count, "H").End(xlUp).Row To 1 Step -1
If Cells(lRow, "H") <> Cells(lRow - 1, "H") Then
Rows(lRow).EntireRow.Insert
End If
Next lRow
Range("A1").Activate
Application.ScreenUpdating = True
End Sub
最佳答案
If Cells(lRow, "I") <> Cells(lRow - 1, "I") Then
当
lRow
不可避免地达到了1
的值那么这将导致错误,因为lRow - 1
是
0
- 并且没有行号 0。您需要针对这种可能性进行编码:
For lRow = Cells(Cells.Rows.Count, "I").End(xlUp).Row To 1 Step -1
If Cells(lRow, "I") <> Cells(lRow - IIf(lRow = 1, 0, 1), "I") Then
Rows(lRow).EntireRow.Insert
End If
Next lRow
应该做的伎俩。
关于vba扫描列如果为0则删除,如果不等于下一行则插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32957797/