Excel VBA删除行如果包含某些string或设置了删除标志

我试图searchB列3个不同的string。 我需要它来search城市,州和邮政编码,如果有任何列有匹配的城市,州或拉链,那么我需要删除该行。

我也有它设置为删除行,如果在D列的字段的开头有一个0,但我不能得到这个工作。

这是我到目前为止的代码:

Sub Removal() Dim i As Long For i = Range("B" & Rows.Count).End(xlUp).Row To 1 Step -1 If Left(Range("D" & i), 1) = "0" Then Rows(i).Delete Else Select Case LCase(Range("B" & i)) Case Is = "Orlando", "FL", "37941" Rows(i).Delete End Select End If Next i End Sub 

代码虽然没有做任何事情。

这是因为您正在对B列中的值进行LCase ,而与Case语句中的TitleCase( "Orlando" )和UpperCase "FL"字相比较。

像这样修改你的代码。 我testing了我的本地Excel,它的工作原理。

更新我也修改了代码来处理你在这里的评论中提到的情况。

 Sub Removal() Dim i As Long, searchString As String For i = Range("B" & Rows.Count).End(xlUp).Row To 1 Step -1 ' if a row is marked for deletion, delete it and continue. If Left(Range("D" & i), 1) = "0" Then Rows(i).Delete ' skip to next row GoTo NextRow End If searchString = LCase(Range("B" & i)) If (InStr(1, searchString, "orlando") > 0) Or _ (InStr(1, searchString, "fl") > 0) Or _ (InStr(1, searchString, "37941") > 0) Then Rows(i).Delete End If NextRow: Next i End Sub 

运行代码之前:

之前

运行代码后:

后