如何在excel中查找包含给定子字符串的单元格中的所有字符串

如何在excel中查找包含给定子字符串的单元格中的所有字符串,excel,Excel,在excel工作表中,每个单元格包含多个字符串。我想找到包含给定子字符串的所有字符串。打印另一个单元格中包含子字符串的所有字符串的公式是什么 例如: 恐怕您无法使用标准的Excel工作表函数实现这一点。您必须像这样在VBA中编写自己的宏 Function matches(ByRef rngCell As Range, ByVal strSearch As String) As String Dim astrWords As Variant Dim strWord As Varia

在excel工作表中,每个单元格包含多个字符串。我想找到包含给定子字符串的所有字符串。打印另一个单元格中包含子字符串的所有字符串的公式是什么

例如:


恐怕您无法使用标准的Excel工作表函数实现这一点。您必须像这样在VBA中编写自己的宏

Function matches(ByRef rngCell As Range, ByVal strSearch As String) As String
    Dim astrWords As Variant
    Dim strWord As Variant
    Dim strResult As String

    'Get the value from the cell and split into words based on the space seperator
    astrWords = Split(CStr(rngCell.Value), " ")
    strResult = ""

    'Loop through the words and check if it contains the string that is searched for
    For Each strWord In astrWords
        If InStr(strWord, strSearch) Then
            'Add it to the result if a match is found
            If strResult <> "" Then
                'Add a space when a previous match was found
                strResult = strResult & " "
            End If
            strResult = strResult & strWord
        End If
    Next strWord
    'Return the result
    matches = strResult
End Function

作为替代方案,您可以使用此自定义项:

Public Function GETMATCHES(ByVal strOriginal As String, ByVal strMatch As String) As String

    GETMATCHES = Join(Filter(Split(WorksheetFunction.Trim(strOriginal), " "), strMatch), " ")

End Function
然后在单元格A2中有一个公式:
=GETMATCHES(A1,“in”)

=matches(A1;"in")
Public Function GETMATCHES(ByVal strOriginal As String, ByVal strMatch As String) As String

    GETMATCHES = Join(Filter(Split(WorksheetFunction.Trim(strOriginal), " "), strMatch), " ")

End Function