我希望代码检查每一行是否为0。如果等于0,则应删除该行。该代码有效,但似乎并不是每行都有。我放入debug.print以显示在运行之前和之后有多少行。如果我多次运行该代码,它将到达标题(不应删除106行)
Dim dblZeilen As Double
dblZeilen = Worksheets("Data").Range("A1", Range("A1").End(xlDown)).Count
Debug.Print dblZeilen
For i = 2 To dblZeilen + 1
If Worksheets("Data").Range("N" & i).Value = 0 Then
Worksheets("Data").Rows(i).Delete
End If
Next i
dblZeilen = Range("A1", Range("A1").End(xlDown)).Count - 1
Debug.Print dblZeilen
debug.print的结果顺序为:
279
279
因此,在第8次迭代运行代码之后,结果是正确的。 我还尝试了另一种方法来运行它,但没有帮助。
Dim dblZeilen As Double
dblZeilen = Worksheets("Data").Range("A1", Range("A1").End(xlDown)).Count
Debug.Print dblZeilen
Dim rng As Range
Dim rngBereich As Range
Set rngBereich = Worksheets("Data").Range("N2:N" & dblZeilen)
Worksheets("Data").Range("N2:N" & dblZeilen).NumberFormat = "0.00"
For Each rng In rngBereich
If rng = 0 Then
rng.Select
ActiveCell.EntireRow.Delete
End If
Next rng
dblZeilen = Worksheets("Data").Range("A1", Range("A1").End(xlDown)).Count
Debug.Print dblZeilen
答案 0 :(得分:0)
替换:
For i = 2 To dblZeilen + 1
具有:
For i = dblZeilen + 1 To 2 Step -1
删除时,请向后运行循环,以免破坏索引。
(可能还有其他问题)