如何查找以特定字母开头的单词并删除该行?

时间:2016-07-14 16:17:01

标签: excel vba excel-vba

我应该使用什么宏来查找以D开头并删除这些行的所有行。 现在我正在使用:(删除TOTAL以上的每个文本),这并不总是完美的。

Sub A2a_Deleterowsabove()
Dim foundOne As Range
On Error Resume Next
With ActiveSheet
    Set foundOne = .Range("A:A").Find(what:="TOTAL", After:=.Range("a1"), LookIn:=xlFormulas, _
                                LookAt:=xlPart, SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, MatchCase:=False)
    If foundOne.Row > 1 Then
        Range(.Range("e1"), foundOne.Offset(-1, 0)).EntireRow.delete shift:=xlUp
    Else

End If
End With
End Sub

我在考虑使用它:

Sub Delete_Cells_with_D()
Dim i As Integer
For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
If Cells(i, 1) = "D* -" Then Cells(i, 1).EntireRow.Delete shift:=xlUp
Next i
End Sub

我应该在宏#2的突出显示部分放置什么来表示D背后的数字是未知的?

Excel example, and Macros #2

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

Sub Delete_Cells_with_D()

    Dim i As Integer
    For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
        If Left(Cells(i, 1), 1) = "D" Then Cells(i, 1).EntireRow.Delete shift:=xlUp
    Next i

End Sub