Excel:检查列与单个单元格的部分匹配
Excel: Check for partial matches from column against single cell
我在 Excel 中有两列:
Column A
Row 1 Apple
Row 2 Blueberry
Row 3 Strawberry
Column B
Row 1 App
Row 2 Application
Row 3 Appendage
我想使用列 B 来查看列 A 中的给定单元格中是否存在其中的任何单元格。到目前为止,我已经使用了 VLOOKUP
和 MATCH
函数,我可以' 似乎可以正常工作,但 MATCH
似乎是我应该使用的那个。我尝试在 B 列上使用通配符,结果 returns 出现值错误。这是我拥有的:
=MATCH(A1,"*"&B:B&"*",0)
非常感谢您的帮助!
有一个自然的 VBA 解决方案。在标准代码模块位置:
Function PartialMatch(v As Variant, R As Range) As Variant
Dim i As Long
For i = 1 To R.Cells.Count
If v Like "*" & R.Cells(i).Value & "*" Then
PartialMatch = i
Exit Function
End If
Next i
PartialMatch = CVErr(xlErrNA)
End Function
然后您可以在电子表格中的任何位置使用公式:
=PartialMatch(A1,B:B)
它将给出第一个部分匹配的索引,如果存在的话,或者 #N/A
如果不存在。请注意,空白单元格算作部分匹配,因此您可能希望确保传递函数的范围不包含空格(因此不要传递整列)。那,或者重新定义你所说的部分匹配。
我在 Excel 中有两列:
Column A
Row 1 Apple
Row 2 Blueberry
Row 3 Strawberry
Column B
Row 1 App
Row 2 Application
Row 3 Appendage
我想使用列 B 来查看列 A 中的给定单元格中是否存在其中的任何单元格。到目前为止,我已经使用了 VLOOKUP
和 MATCH
函数,我可以' 似乎可以正常工作,但 MATCH
似乎是我应该使用的那个。我尝试在 B 列上使用通配符,结果 returns 出现值错误。这是我拥有的:
=MATCH(A1,"*"&B:B&"*",0)
非常感谢您的帮助!
有一个自然的 VBA 解决方案。在标准代码模块位置:
Function PartialMatch(v As Variant, R As Range) As Variant
Dim i As Long
For i = 1 To R.Cells.Count
If v Like "*" & R.Cells(i).Value & "*" Then
PartialMatch = i
Exit Function
End If
Next i
PartialMatch = CVErr(xlErrNA)
End Function
然后您可以在电子表格中的任何位置使用公式:
=PartialMatch(A1,B:B)
它将给出第一个部分匹配的索引,如果存在的话,或者 #N/A
如果不存在。请注意,空白单元格算作部分匹配,因此您可能希望确保传递函数的范围不包含空格(因此不要传递整列)。那,或者重新定义你所说的部分匹配。