我正在尝试删除A列中ID字段中没有“AB”或“ON”的整行。我已经编写了下面的代码,但它删除了所有行。任何人都可以帮助我。我附上了数据截图,谢谢! Data Screenshot link:
Sub DeleteRows()
FinalRow = Cells(Rows.Count, "A").End(xlUp).Row
i = 2
Do While i <= FinalRow
If Cells(i, "A") <> "PQ*" OR If Cells(i, "A") <> "ON*" Then
Cells(i, 1).EntireRow.Delete
FinalRow = FinalRow - 1
Else
i = i + 1
End If
Loop
End Sub
答案 0 :(得分:2)
根据我的评论:
Sub DeleteRows()
FinalRow = Cells(Rows.Count, "A").End(xlUp).Row
i = 2
Do While i <= FinalRow
If Left(Cells(i, "A"), 2) = "PQ" Or Left(Cells(i, "A"), 2) = "ON" Then
i = i + 1
Else
Cells(i, 1).EntireRow.Delete
FinalRow = FinalRow - 1
End If
Loop
End Sub
答案 1 :(得分:1)
Like
运营商。OR
括在括号中如果您使用的是我在原始帖子中理解的外卡,它会转换为以 PQ或ON
开头的单元格所以:
Option Explicit
Sub DeleteRows()
Dim FinalRow As Long
Dim I As Long
FinalRow = Cells(Rows.Count, "A").End(xlUp).Row
For I = FinalRow To 2 Step -1
If Not (Cells(I, "A") Like "PQ*" Or _
Cells(I, "A") Like "AB*") Then _
Cells(I, 1).EntireRow.Delete
Next I
End Sub
答案 2 :(得分:0)
这是一个与你正在做的更相似的选项。
已编辑:如果ID是前两个字母,则应该有效。
Sub Deleterows()
FinalRow = Cells(Rows.count, "A").End(xlUp).row
i = 2
Do While i <= FinalRow
If Not Left(Cells(i, "A"), 2) = "PQ" And Not Left(Cells(i, "A"), 2) = "ON" Then
Cells(i, 1).EntireRow.Delete
FinalRow = FinalRow - 1
Else
i = i + 1
End If
Loop
End Sub