我想按此顺序执行此功能:
1.激活自动过滤功能
2.在M栏中,勾选“存款逆转”和“空白”
3.删除C列中包含“AQ *”,“AI *”,“BG”的单元格的整行。(注意:*表示字母后面的数字)
我尝试使用自动过滤器进行宏录制,但它只删除指定范围内的行(如果我使用其他数据集,则可能会有所不同)。 VBA如下。
有没有更简单/更好的方式来执行此操作?
感谢您的帮助!
Sub Macro4()
'
' Macro4 Macro
'
'
Rows("1:1").Select
Application.CutCopyMode = False
Selection.AutoFilter
ActiveSheet.Range("$A$1:$N$46437").AutoFilter Field:=13, Criteria1:= _
"=Deposit Reversed", Operator:=xlOr, Criteria2:="="
ActiveSheet.Range("$A$1:$N$46437").AutoFilter Field:=3, Criteria1:=Array( _
"AQ", "AQ01E166N", "AQ01E294N", "AQ01E316N", "AQ01E373N"), Operator:= _
xlFilterValues
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveSheet.Range("$A$1:$N$46017").AutoFilter Field:=3, Criteria1:=Array( _
"AI", "AI04_MMRASHI_TWT", "AI04E230N", "AI04E269N", "AI04E323N"), Operator:= _
xlFilterValues
ActiveWindow.SmallScroll Down:=-6
Rows("10236:10236").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveSheet.Range("$A$1:$N$45998").AutoFilter Field:=3, Criteria1:=Array( _
"BG", "BG01A004", "BG01H082", "BG01H106N"), Operator:=xlFilterValues
ActiveWindow.SmallScroll Down:=-3
Rows("5:5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
End Sub
答案 0 :(得分:1)
将通配符过滤条件放入数组中并循环显示它们。您无法找到更快的删除行的方法,只要在删除行之前检查是否存在要删除的行,它就是安全的。
Dim c As Long, vCRITC As Variant
vCRITC = Array("AQ*", "AI*", "BG*")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
With Sheets("Sheet7").Cells(1, 1).CurrentRegion
If .AutoFilter Then .AutoFilter
With .Resize(.Rows.Count, Columns("A:N").Count)
.AutoFilter Field:=13, Criteria1:="=Deposit Reversed", Operator:=xlOr, Criteria2:="="
For c = LBound(vCRITC) To UBound(vCRITC)
.AutoFilter Field:=3, Criteria1:=Chr(61) & vCRITC(c)
With .Offset(1, 0)
If CBool(Application.Subtotal(103, .Columns(3))) Then _
.Rows.Delete
End With
.AutoFilter Field:=3
Next c
End With
.AutoFilter
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
原始问题中似乎有一个拼写错误,其中不包含 BG 的星号通配符。这可能与组合编辑器使用星号来记录斜体有关。这首先在M列上设置标准,然后将每个通配符标准添加(并删除)到C列,删除它找到的行。
答案 1 :(得分:0)
不需要为此目的使用自动过滤器
使用此
Sub test()
Dim i&
i = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
With ActiveSheet
While i <> 1
If .Cells(i, "M").Value = "deposit reversed" Or .Cells(i, "M").Value = "" Then
If UCase(.Cells(i, "C").Value) Like "AQ*" Or _
UCase(.Cells(i, "C").Value) Like "AI*" Or _
UCase(.Cells(i, "C").Value) Like "BG*" Then
.Rows(i).Delete
End If
End If
i = i - 1
Wend
End With
End Sub