在Excel中提取部分匹配2列之间的字符串(单词)

时间:2018-03-07 02:56:45

标签: excel excel-formula user-defined-functions

我有两个列,如下所示,我需要提取一个在2列之间匹配的部分单词并将其放在第3列中。

      Name1                              Name2
RED CURRY CHICKEN                 GREEN CURRY CHICKEN
BEEF WITH MINT LEAVES             BEEF WITH BASIL LEAVES
SWEET AND SOUND PORK              BACON AND EGG
FRIED RICE                        FRIED RICE

我的预期结果

      Name1                              Name2                     Partial Matches
RED CURRY CHICKEN                 GREEN CURRY CHICKEN               CHICKEN
BEEF WITH MINT LEAVES             BEEF WITH BASIL LEAVES            BEEF WITH LEAVES
SWEET AND SOUND PORK              BACON AND EGG                     AND
FRIED RICE                        FRIED RICE                        FRIED RICE

1 个答案:

答案 0 :(得分:1)

用公式做到这一点是效率低下的;你需要vba用于子程序或用户定义的函数。

Function friedRice(str1 As String, str2 As String)
    Dim w As Long, words As Variant, tmp As String

    words = Split(str1, Chr(32))

    For w = LBound(words) To UBound(words)
        If Not IsError(Application.Match(words(w), Split(str2, Chr(32)), 0)) Then
            tmp = tmp & Chr(32) & words(w)
        End If
    Next w

    friedRice = Trim(tmp)

End Function

enter image description here