以下是我的代码。我试图移动一些行,如果那里有某些数据,那么删除那些行,但是我编写它的当前方式,每当我删除一行时,我跳过它下面的那一行。我将范围更改为高编号行到低编号行,但我的宏仍然从顶部开始向下移动。我想如果我把它向上移动,删除不会导致它跳过下一个项目。如何让它从列表底部向上移动,或者更好的方法是什么? 我的代码如下:
Dim lLastRow As Long
Dim num As Integer
Dim name As String
Dim rCell As Range
Dim afCell As Range
Dim rRng As Range
Dim affectedRng As Range
Windows("Most Affected Customer Info.xls").Activate
Worksheets("Sheet 1").Activate
Cells(1, 1).Select
Selection.End(xlDown).Select
lLastRow = ActiveCell.Row
Set affectedRng = Range("A" & lLastRow & ":A2")
'First remove resolved entries
For Each afCell In affectedRng
If (afCell.Offset(0, 4).Value = "resolved" Or afCell.Offset(0, 4).Value _ = "Resolved" Or afCell.Offset(0, 2).Value = "Resolved" Or afCell.Offset(0, 2).Value = _ "resolved") Then
afCell.EntireRow.Delete
End If
Next afCell
答案 0 :(得分:4)
向后浏览列表怎么样?
编辑一些代码尝试
For row = lLastRow To 1 Step -1
If Range("D" & row).Value = "resolved" Then Rows(row).EntireRow.Delete
Next row
我在一个小案例中对此进行了测试,在D栏中“已解决”,它就像一个冠军。您可能会发现代码都可以完成并且读取效果很好。
答案 1 :(得分:3)
您需要一个do循环并使用行号,因为您需要操纵循环中的当前位置以及结束点;
e.g。
Dim lRow as Long
lRow = 1
Do Until lRow > lLastRow
Set afCell = Cells(lRow ,1)
If (afCell.Offset(0, 4).Value = "resolved" Or afCell.Offset(0, 4).Value _ = "Resolved" Or afCell.Offset(0, 2).Value = "Resolved" Or afCell.Offset(0, 2).Value = _ "resolved") Then
afCell.EntireRow.Delete
'Decrement the last row as we've removed a row
lLastRow = lLastRow - 1
else
'Increment the row number to move to the next one
lRow = lRow + 1
End IF
Loop
注意:这是完全未经测试的,所以你需要调试它,但你应该得到要点。
答案 2 :(得分:0)
您可以尝试使用While循环更改If条件。
For Each afCell In affectedRng
r = afCell.Row
c = afCell.Column
While (afCell.Offset(0, 4).Value = "resolved" Or afCell.Offset(0, 4).Value _ = "Resolved" Or afCell.Offset(0, 2).Value = "Resolved" Or afCell.Offset(0, 2).Value = _ "resolved")
afCell.EntireRow.Delete
Set afCell = Cells(r, c)
Wend
Next afCell
答案 3 :(得分:0)
你可以用do while替换你的For Each,如下所示:
rowx = 2
Do While rowx < llastrow
If Range("B" & rowx).Value = "resolved" Then 'replace this with the columns you're checking
Rows(rowx).EntireRow.Delete
Else
rowx = rowx + 1
End If
Loop