# 删除在Excel中没有特定文本的整个行Delete Entire Rows which do not have certain Text in Excel

``````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
``````

## 3 个回复3

### ===============>>#1 票数：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
``````

### ===============>>#2 票数：1

• 删除行时，最好从下往上
• 通配符不能与相等运算符一起使用； 您应该使用`Like`运算符。
• 你的逻辑有点偏离。 您需要将“ `OR` ”括在括号中

``````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
``````

### ===============>>#3 票数：0

``````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
``````