简体   繁体   English

Excel VBA:如果列A在字符串中包含数字,则删除行

[英]Excel VBA: Delete row if Column A contains a number within the string

I am trying to write a formula in Excel to delete an entire row if the cell in Column C contains a number within the cell, except the number 0. 我正在尝试在Excel中编写公式以删除整行,如果C列中的单元格包含该单元格中的数字(数字0 除外)

eg 例如

A2 - delete
A0 - don't delete
M - don't delete
ABC6 - delete
NN - don't delete


Sub FindInvalid()
    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim JEType As Variant

    InvalidCharacters= Array(1, 2, 3, 4, 5, 6, 7, 8, 9)

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    With ActiveSheet

        .Select

        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView

        .DisplayPageBreaks = False

        Firstrow = .UsedRange.Cells(1).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

        For Lrow = Lastrow To Firstrow Step -1

            With .Cells(Lrow, "C")

                If Not IsError(.Value) Then
                Debug.Print (.Value)

                    If IsInArray(.Value, InvalidCharacters) = True Then .EntireRow.Delete
                    'This will delete each row where Column C contains a number

                End If

            End With

        Next Lrow

    End With

    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With

End Sub

The above works great, but I'm struggling with the below. 上面的方法很棒,但是我在下面的方法上苦苦挣扎。

Function IsInArray(StringToBeFound As String, arr As Variant) As Boolean
  IsInArray = Not IsError(Application.Find(StringToBeFound, "contains", arr, 0))

End Function

Any help would be greatly appreciated. 任何帮助将不胜感激。

You are only checking a single cell at a time to see if it contains certain digits. 您一次只检查一个单元格以查看其是否包含某些数字。

Instead of the IsInArray function, perhaps: 代替IsInArray函数,也许:

     With .Cells(Lrow, "C")

            If Not IsError(.Value) Then
            Debug.Print (.Value)

                If .Value Like "*[1-9]*" Then .EntireRow.Delete
                'This will delete each row where Column C contains a number

            End If

        End With

EDIT: For large worksheets, this may not be the most efficient method. 编辑:对于大型工作表,这可能不是最有效的方法。 One possibility would be to use the Advanced Filter. 一种可能性是使用高级过滤器。

For the Advanced Filter, you can use a formula for the criteria: 对于“高级筛选器”,可以将公式用作条件:

  =NOT(OR(ISNUMBER(FIND({1,2,3,4,5,6,7,8,9},C9))))

You'll probably want to copy the results to another location. 您可能需要将结果复制到另一个位置。

A VBA routine that might work faster: 一个可能运行得更快的VBA例程:

Algorithm 算法

  • Read Column C into a variant array 将C列读入变量数组
  • process each item to see if it meets the criteria to be deleted 处理每个项目以查看其是否符合要删除的条件
  • Collect the row number for each item to be deleted Collect要删除的每个项目的行号
  • Use the Union method to create a range to be deleted 使用Union方法创建要删除的范围
  • Delete the range 删除范围

Option Explicit
Sub delNumRows()
    Dim V As Variant
    Dim COL As Collection
    Dim I As Long
    Dim R As Range

V = Range(Cells(1, 3), Cells(Rows.Count, 3).End(xlUp))

Set COL = New Collection
For I = 1 To UBound(V)
    If V(I, 1) Like "*[1-9]*" Then COL.Add I
Next I

For Each V In COL
    If R Is Nothing Then
        Set R = Cells(V, 1).EntireRow
    Else
        Set R = Union(R, Cells(V, 1).EntireRow)
    End If
Next V

R.Delete

End Sub

standing the "structrure" of your array to be searched in, you could use this: 站在要搜索的数组的“结构”中,可以使用以下命令:

Function IsInArray(StringToBeFound As String, arr As Variant) As Boolean
    IsInArray = InStr("|" & Join(arr, "|") & "|", "|" & StringToBeFound & "|") > 0
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM