+ Reply to Thread
Results 1 to 5 of 5

Extract n words from string

  1. #1
    Registered User
    Join Date
    12-02-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    32

    Extract n words from string

    I'm using the following formula to extract the first five words from a string in cell A1.

    =TRIM(LEFT(A1,FIND("^",SUBSTITUTE(TRIM(A1)&" "," ","^",5))-1))

    It works great except when the string has fewer than 5 words in it, in which case it returns a "Value#" error.

    Any suggestions on how to edit this so it can handle strings that are only 1, 2, 3, or 4 words long?

  2. #2
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extract n words from string

    =if(len(a2)-len(substitute(a2," ",""))<4,a2,trim(left(a2,find("^",substitute(trim(a2)&" "," ","^",5))-1)))
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  3. #3
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Extract n words from string

    This small change should do it

    =TRIM(LEFT(A1,FIND("^",SUBSTITUTE(TRIM(A1)&REPT(" ",5)," ","^",5))-1))
    Audere est facere

  4. #4
    Registered User
    Join Date
    12-02-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    32

    Re: Extract n words from string

    Quote Originally Posted by daddylonglegs View Post
    This small change should do it

    =TRIM(LEFT(A1,FIND("^",SUBSTITUTE(TRIM(A1)&REPT(" ",5)," ","^",5))-1))
    That works great! On a related note, I'm now trying to pick up where that formula leaves off, and extract the *next* five words from the string, and so forth.

    So if the string in A1 is "The quick brown fox jumps over the lazy dog." then I want

    B1 = The quick brown fox jumps
    C1 = over the lazy dog.

    But perhaps there's a better approach. The reason for all this is I have a text box (Excel 2007, so Insert > Text Box) that gets its value from a formula (=A1). But as I understand it there's no decent way to enable word wrap in a text box that gets its values from a formula. My text box is only wide enough to show about 5 words at a time, so I was going to break the original string (which may vary from 1 to 20 words) into at most four 5-word lines, and use four text boxes (=B1, =C2, =D2, =E2) to show the result. I have to assume there's a better way but I've searched to no avail.

  5. #5
    Registered User
    Join Date
    12-02-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    32

    Re: Extract n words from string

    Strange. I just recreated the text boxes and the words now wrap just fine.

    Thanks anyway for the help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1