如果单元格以特定数字开头,则删除行的宏

时间:2018-08-21 16:29:56

标签: excel vba excel-vba

我正在寻找一个宏,如果一列中的单元格以特定数字开头,则该宏可以删除整行。

例如,如果列A中的单元格以数字4或3开头,我希望宏删除整个行

Col A
4563 - Delete
3598 - Delete
4598 - Delete
9582
8574
7585
3254 - Delete

感谢您的帮助!

问候 西米

1 个答案:

答案 0 :(得分:0)

对于Column A(不包括标题的第一行)...

Option Explicit

Sub Delete_3_4()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")

Dim SearchRange As Range, SearchCell As Range, DeleteMe As Range
Set SearchRange = ws.Range("A2:A" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)

For Each SearchCell In SearchRange
    If Left(SearchCell, 1) = 3 Or Left(SearchCell, 1) = 4 Then
        If DeleteMe Is Nothing Then
            Set DeleteMe = SearchCell
        Else
            Set DeleteMe = Union(DeleteMe, SearchCell)
        End If
    End If
Next SearchCell

If Not DeleteMe Is Nothing Then DeleteMe.EntireRow.Delete

End Sub