将文本两个单词提取到字符串中的Delimiter值的LEFT

时间:2015-11-15 00:53:32

标签: excel extract formula

此问题与以前发布的Excel问题不重复,而是这个问题正在寻求提取分隔符的LEFT词;而之前发布的问题将词语提取到分隔符权利。

使用MS Excel,我想提取两个单词(三个空格)到分隔符值的左边" ^",包括提取带有附加/关联分隔符的标识符单词" ^ "

示例:单元格A2

约翰尼和我打算去电影看^蝙蝠侠归来,但现在已经晚了。

结果:细胞B2

看^蝙蝠侠

2 个答案:

答案 0 :(得分:1)

可能有更好的解决方案,但这就是我所拥有的:

=MID(LEFT(A1,FIND(" ",A1;FIND("^",A1))-1),FIND("^",SUBSTITUTE(A1," ","^",LEN(LEFT(A1,FIND("^",A1)))-LEN(SUBSTITUTE(LEFT(A1,FIND("^",A1))," ",""))-2))+1,9999)

核心是:LEN(LEFT(A1,FIND("^",A1)))-LEN(SUBSTITUTE(LEFT(A1,FIND("^",A1))," ",""))
它计算从字符串开始到你的分隔符的空格,然后用anoter分隔符替换count - 2空格#来找到你的开始FIND("^",SUBSTITUTE(A1," ","^",LEN(...)-LEN(...)-2))+1为一个早先结束的字符串LEFT(A1,FIND(" ",A1;FIND("^",A1))-1)执行此操作(你不需要)第二次做长期的事情)

答案 1 :(得分:0)

非VBA公式方法是可能的(可能作为数组公式),但就像用黄油刀制作雕塑一样。这是一个可以用作UDF的VBA函数:

Function ExtractLeft(str As String, delim As String, words As Long) As String
    Dim i As Long, n As Long
    Dim A As Variant
    Dim left_words As String, right_word As String

    A = Split(str, delim)
    right_word = A(1)
    right_word = Split(right_word)(0)
    right_word = delim & right_word
    left_words = A(0)
    A = Split(Trim(left_words))
    n = UBound(A)
    For i = n To n - words + 1 Step -1
        right_word = A(i) & " " & right_word
    Next i
    ExtractLeft = right_word
End Function

VBA方法的优点是您可以轻松更改分隔符和要提取的单词数:

enter image description here