我正在尝试使用VBA自动过滤项目。
问题是每个列表项之间都有斜杠。例如,(ABC / EFG / HIJ)。我想拉出包含字符串" ABC,"的列表项。是单独还是与其他字符串项一起列出。例如,如果一个列表项(ABC / EFG)和另一个列表项(ABC),我想要这两个项,因为这两个项本身都包含ABC字符串。这就是我到目前为止所做的:
Sub FilterByABC()
'Change this to the relevant worksheet
Set ws = ThisWorkbook.Sheets("ABC")
Worksheets("ABC").Range("A1").AutoFilter , Field:=1, Criteria1:="ABC"
Operator = xlFilterValues
Range("A1").Select
Columns("A").Copy
Sheets("ABCData").Select
Columns("A").Select
Worksheets("ABCData").Paste
End Sub
答案 0 :(得分:1)
添加第二个通配符过滤器和Operator:=xlAnd
参数。
Option Explicit
Sub FilterByABC()
Dim rngDest As Range
With Worksheets("ABCData")
Set rngDest = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With
With Worksheets("ABC")
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells(1, 1).CurrentRegion
.AutoFilter Field:=1, Criteria1:="ABC*", _
Operator:=xlAnd, Criteria2:="<>ABC EC*"
With .Resize(.Rows.Count - 1, 1).Offset(1, 0)
If CBool(Application.Subtotal(103, .Cells)) Then
'there are visible, filtered cells in column A; copy then to ABCData
.SpecialCells(xlCellTypeVisible).Copy Destination:=rngDest
End If
End With
End With
If .AutoFilterMode Then .AutoFilterMode = False
End With
End Sub
答案 1 :(得分:0)
您应该添加两个标准来实现这一目标:
Sub FilterByABC()
'Change this to the relevant worksheet
Set ws = ThisWorkbook.Sheets("ABC")
Worksheets("ABC").Range("A1").AutoFilter , Field:=1, _
Criteria1:="ABC*", Operator:= xlAnd, Criteria2:="<>ABD EC"
Operator = xlFilterValues
Range("A1").Select
Columns("A").Copy
Sheets("ABCData").Select
Columns("A").Select
Worksheets("ABCData").Paste
End Sub