使用 excel 公式在单元格中查找确切的子字符串

Find exact substring in cell using excel formula

我在一个单元格中有字符串 "L5+L6+L7+L10",然后在每个单元格中有一列的值为 L1、L2、...、L10。我需要知道哪些值包含在主字符串中但完全匹配。

为了解决我的问题,我尝试了公式 =IF(ISNUM(SEARCH(B3;$F));"Found";"Not found"),得到的结果如图所示。

但是,这个结果是不正确的,因为我只需要找到 L5、L6、L7 和 L10,而不是 L1。

有没有办法只使用 excel 公式来做到这一点?

FILTERXML 函数可以在这种情况下提供帮助:

=SUMPRODUCT(--(B3=FILTERXML("<data><a>" & SUBSTITUTE($F;"+";"</a><a>") & "</a></data>";"//a")))

任何查看问题并愿意接受 VBA UDF 解决方案的人

Post 代码成作品sheet 模块

公式语法:=Found( Substring, Search String )

所以在 excel sheet 上:C3 = Found(B3, F2) = Not Found


Public Function Found(SubString As Range, Within As Range) As String

Dim Arr, i As Long
Arr = Split(Within, "+")

Found = "Not Found"

For i = LBound(Arr) To UBound(Arr)
    If SubString = Arr(i) Then
        Found = "Found"
        Exit For
    End If
Next i

End Function

只需将 "+" 添加到搜索中的字符串的开头和结尾:

=IF(ISNUMBER(SEARCH("+"&B3&"+","+"&$F&"+")),"Found","Not found")