Results 1 to 7 of 7

Thread: Find the First or Last So Many Words in a Text String

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    14
    Did you consider; to return the first 4 words:

    PHP Code:
    =LEFT(A1;FIND("@";SUBSTITUTE(A1;" ";"@";4))) 
    The N in your formula is 4 in mine (as an example)

    PHP Code:
    =LEFT(A1;FIND("@";SUBSTITUTE(A1;" ";"@";N))) 
    The last N words:

    PHP Code:
    =MID(TRIM(A1);FIND("@";SUBSTITUTE(TRIM(A1);" ";"@";1+LEN(A1)-LEN(SUBSTITUTE(TRIM(A1);" ";""))-3));LEN(A1)) 
    Last edited by snb; 06-19-2012 at 06:07 PM.

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by snb View Post
    Did you consider; to return the first 4 words:

    PHP Code:
    =LEFT(A1;FIND("@";SUBSTITUTE(A1;" ";"@";4))) 
    The N in your formula is 4 in mine (as an example)

    PHP Code:
    =LEFT(A1;FIND("@";SUBSTITUTE(A1;" ";"@";N))) 
    Nope, at 5:00AM in the morning, that method simply didn't occur to me. Thanks for reminding me of it, although you should see my note in the next section.

    Quote Originally Posted by snb View Post
    The N in your formula is 4 in mine (as an example)

    PHP Code:
    =LEFT(A1;FIND("@";SUBSTITUTE(A1;" ";"@";N))) 
    This is a better formula than mine, for sure, but it is including a trailing space character. You need to subtract 1 from the FIND function's result...

    PHP Code:
    =LEFT(A1,FIND("@",SUBSTITUTE(A1," ","@",3))-1
    Quote Originally Posted by snb View Post
    The last N words:

    PHP Code:
    =MID(TRIM(A1);FIND("@";SUBSTITUTE(TRIM(A1);" ";"@";1+LEN(A1)-LEN(SUBSTITUTE(TRIM(A1);" ";""))-3));LEN(A1)) 
    In a similar fashion to your other formula, this one is including a leading space in its return value. However, with 6 more function calls than the formula I posted for this situation, I think I'll stay with my formula for this case.

Similar Threads

  1. Get Field from Delimited Text String
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 5
    Last Post: 07-25-2025, 08:35 AM
  2. Replies: 14
    Last Post: 05-25-2013, 06:55 AM
  3. Extract Certain Characters From A Text String
    By bobkap in forum Excel Help
    Replies: 5
    Last Post: 05-24-2013, 06:25 AM
  4. Find Color in the string using formula.
    By LalitPandey87 in forum Excel Help
    Replies: 4
    Last Post: 07-10-2012, 09:16 PM
  5. Replies: 6
    Last Post: 09-26-2011, 07:39 AM

Tags for this Thread

Posting Permissions

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