有没有办法提取包含特定单词的所有句子?

时间:2019-02-17 13:34:29

标签: excel

我想提取所有包含“苹果”一词的句子,包括小写和大写。结果可以一起显示在单元格B1中,也可以分成其他单元格。

在单元格A1中采样文本: 一天一苹果,医生远离我。但是我不认为他们很好吃。我从来不喜欢他们。苹果不是我的最爱,梨是。

是否有Excel公式?谢谢。

2 个答案:

答案 0 :(得分:1)

这可用于Open Office,Google文档,Excel等:

=IF(ISNUMBER(SEARCH("apple",A22)),A22,"")

它给出以下输出:

Example of function use in spreadsheet

答案 1 :(得分:0)

句子字符串搜索

您可能不愿花很长的公式写几句话,但我认为这不会解决您的任务。因此,我开发了一个用户定义函数(UDF)。将代码复制到标准模块中。

Function SSS(ByVal SearchString As String, ByVal SearchText As String, _
        Optional ByVal SplitString As String = ".", _
        Optional ByVal JoinString As String = " ") As String

    Dim vnt As Variant  ' String Array
    Dim i As Long       ' String Array Element Counter
    Dim strB As String  ' String Builder

    vnt = Split(SearchText, SplitString)
    ' Loop through elements of String Array.
    For i = 0 To UBound(vnt) - 1 ' "-1" ie. Ignore text after last Split String.
        ' Check value of current element of String Array against SearchString.
        ' "vbTextCompare" determines case-INsensitivity (AA = aa = Aa = aA).
        If InStr(1, vnt(i), SearchString, vbTextCompare) Then
            ' Build the String
            If strB <> "" Then ' For all but the first found SearchString.
                strB = strB & JoinString & Trim(vnt(i)) & SplitString
              Else             ' Only for the first found SearchString.
                strB = Trim(vnt(i)) & SplitString
            End If
        End If
    Next

    SSS = strB

End Function

在Excel中的用法

A1包含"apple"
A2包含"An apple a day keeps the doctor away. But I don’t think they are yummy. I have never liked them. Apples aren’t my favourite, pears are."

在其他任何单元格中,写以下公式:

=SSS(A1, A2)

获得以下结果:

An apple a day keeps the doctor away. Apples aren’t my favourite, pears are.

备注

根据您的情况,您可以省略第3和第4个参数的参数:

  • 第三个参数为SplitString,默认情况下为DOT“。”
  • 第四个参数为JoinString,默认情况下为SPACE“”。