我想使用自动过滤器在两列中进行部分匹配,它不适用于两列,但适用于一列。
我可以知道如何用字符过滤数字吗?
如 Mws.Range("E5").Value 包含数字和字符或仅字符,例如 YT-154895、WS-248350 和缓冲区。
另外,Mws.Range("G5").Value 仅为数字,例如 4501236852。
我可以知道如何修改代码吗?
Sub Search()
Dim Mws As Worksheet
Dim PR As Worksheet
Dim Rng As Range
Set Mws = ThisWorkbook.Sheets("Data input v2")
Set PR = ThisWorkbook.Sheets("Project Record")
Mws.Range("C19:AW9999").ClearContents
Set Rng = PR.Range("D2:AX" & PR.Cells(PR.Rows.Count, "AX").End(xlUp).Row)
With Rng
'Rng.AutoFilter Field:=4, Criteria1:=Mws.Range("E5").Value
'Rng.AutoFilter Field:=5, Criteria1:=Mws.Range("G5").Value
Rng.AutoFilter Field:=4, Criteria1:="*" & Mws.Range("E5").Value & "*"
Rng.AutoFilter Field:=5, Criteria1:="*" & Mws.Range("G5").Value & "*"
Rng.Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy Mws.Range("C19")
Rng.Parent.AutoFilterMode = False
End With
End Sub
如果我使用下面的公式,它可以工作,但我不知道如何转换为 VBA 代码,使其可作为过滤器公式进行编辑
=IFS(I9="Partial Match",FILTER('Project Record'!D3:AX99999,ISNUMBER(SEARCH(E5,'Project Record'!G3:G99999))*ISNUMBER(SEARCH(G5,'Project Record'!H3:H99999)),"No Match Found"))
有人帮忙吗?
非常感谢。
如果我正确理解你想要做什么,这在我的样本表中有效。
Sub two_column_partial_match()
Dim rng As Range
Dim startData As Variant
Dim resultData As Variant
Dim i As Long
Dim lastrow As Long
Dim k As Long
Dim j As Long
Range("C19:K1048576").Clear
lastrow = Worksheets("Project Record").Range("G" &
Rows.Count).End(xlUp).Row
Set rng = Worksheets("Project Record").Range("G3:O" & lastrow)
startData = rng.Value
ReDim resultData(1 To rng.Rows.Count, 1 To 9)
k = 1
For i = 1 To UBound(startData, 1)
If InStr(1, startData(i, 1), Cells(5, 5), 1) > 0 And InStr(1,
startData(i, 2), Cells(5, 7), 1) > 0 Then
For j = 1 To 9
resultData(k, j) = startData(i, j)
Next j
k = k + 1
Else
End If
Next i
Range("C19").Resize(k, 9) = resultData
End Sub