VBA Deleting rows with the Like operator

前端 未结 3 1511
借酒劲吻你
借酒劲吻你 2020-12-12 06:17

Simple thing is not simple. I am trying to delete rows based on a specific column having data that begins with \"2L\". So I wrote this code (LastRow is understood):

3条回答
  •  情深已故
    2020-12-12 07:07

    Or use autofilter

    Option Explicit
    Public Sub CleanUp()
        Dim lastRow As Long, testRange As Range
        With ThisWorkbook.Worksheets("Sheet2")
            lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
            Set testRange = .Range("F1:F" & lastRow)
            If Application.WorksheetFunction.CountIf(testRange, "2L*") > 0 Then
                With testRange
                    .AutoFilter
                    .AutoFilter Field:=1, Criteria1:="2L*", Operator:=xlFilterValues
                    .SpecialCells(xlCellTypeVisible).EntireRow.Delete
                End With
            End If
        End With
    End Sub
    

提交回复
热议问题