如果单元格的单元格值不是以数字开头,如何删除整行

时间:2018-07-30 19:26:27

标签: excel vba excel-vba

感谢您对以下查询的帮助!

我希望创建一个宏,如果A列的单元格中的值不是以数字开头,那么它将帮助我删除整行。

例如,如果A列中的值如下:-

Column A  
12345    Good
A1234    Delete
D5640    Delete
54689    Good

问候 西米

1 个答案:

答案 0 :(得分:0)

您可以测试第一个字符Isnumeric。使用Union收集不以数字开头的行,并一次性删除所有行。

Option Explicit

Public Sub DeleteRows()
    Dim rng As Range, unionRng As Range
    Application.ScreenUpdating = False
    With Worksheets("Sheet1")
        For Each rng In .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row) 'Or A2 to skip headers
            If Not IsNumeric(Left$(rng.Value, 1)) Then
                If Not unionRng Is Nothing Then
                    Set unionRng = Union(unionRng, rng)
                Else
                    Set unionRng = rng
                End If
            End If
        Next
    End With
    If Not unionRng Is Nothing Then unionRng.EntireRow.Delete
    Application.ScreenUpdating = True
End Sub