Extract a Word from String Containing a Specific Character within Substring

前端 未结 2 1061
南方客
南方客 2021-01-16 10:46

In MS Excel I would like to use a formula to extract only the word from a cell that contains a specific character (\"=\") within the text.

A2: Dolly made me a homem

相关标签:
2条回答
  • 2021-01-16 11:30

    I used 5 helper columns to do this; they could be collapsed into one very long forumula, but I prefer it this way (easier to track down source of any errors I think). Given the strings you have in A2; In B2 this finds location of "="

    =SEARCH("=",A2)  
    

    In C2 this finds the instance number of the " " preceeding the "="

    =B2-LEN(SUBSTITUTE(MID(A2,1,B2)," ",""))  
    

    In D2 this notes the location of that " "

    =SEARCH(CHAR(33),SUBSTITUTE(A2," ",CHAR(33),C2))  
    

    In E2 this locates the " " trailing the "=", or if the "=" appears in the final word in the cell it notes the full cell length +1

    =IFERROR(SEARCH(" ",A2,B2),LEN(A2)+1)  
    

    Using these values in F2 this pulls the string from the preceeding " " to the trailing " "

    =MID(A2,D2+1,E2-D2-1)
    
    0 讨论(0)
  • 2021-01-16 11:40

    Use an old text parsing trick that greatly increases the distance between the words with repeating zeroes through the SUBSTITUTE and REPT functions which affords a larger swipe of the intended substring.

          

    The formula in B2 is,

    =TRIM(MID(SUBSTITUTE(A2, " ", REPT(" ", 99)), MAX(1, FIND("=", SUBSTITUTE(A2, " ", REPT(" ", 99)))-50), 99))
    

    The TRIM function (used as a wrapper) removes leading and trailing spaces.

    0 讨论(0)
提交回复
热议问题