<><>><>>说明:<>>>> (Thanks for @MGonet s comments)
- As wildcards are used, it only filters text and number as text. That is, it doesn work for numbers in the column.
Operator:=xlFilterValues
doesn t support wildcards criterias. It is converted to xlOr
if there are only two items (with wildcards) in MyArray
.
rng.AutoFilter Field:=1, Criteria1:=MyArray(0), Criteria1:=MyArray(1), Operator:=xlOr
rng
is a range object of Column. There isn t more space (cells) on worksheet for rng.Offset(1, 0)
.
- Use
End(xlUp)
to get the last used cell on the column
- It s more reliable to resize range before using offset.
rng.Resize(rng.Rows.Count - 1).Offset(1, 0)
OERN
is used to ignore error when there isn t any visible rows after filtering.
Sub FilterAndDelete(ws As Worksheet, col As String, ParamArray criteria() As Variant)
Dim rng As Range, lastRow As Long, c As Range
Dim MyArray As Variant
MyArray = criteria
With ws
If .AutoFilterMode Then .AutoFilter.ShowAllData
Set rng = .Range(.Cells(1, col), .Cells(.Rows.Count, col).End(xlUp))
End With
If UBound(MyArray) = 1 Then
rng.AutoFilter Field:=1, Criteria1:=MyArray(0), Criteria1:=MyArray(1), Operator:=xlOr
Else
Doesn t support wildcards if more than 2 items in MyArray
rng.AutoFilter Field:=1, Criteria1:=MyArray, Operator:=xlFilterValues
End If
On Error Resume Next OERN for blank
Set c = rng.Resize(rng.Rows.Count - 1).Offset(1, 0).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not c Is Nothing Then
c.EntireRow.Delete
End If
ws.AutoFilterMode = False
End Sub
Microsoft documentation:
结束语: