+ Reply to Thread
Results 1 to 12 of 12

Extracting the first, middle and last words in a string / sentence

  1. #1
    Registered User
    Join Date
    05-16-2020
    Location
    Lima
    MS-Off Ver
    2019
    Posts
    20

    Extracting the first, middle and last words in a string / sentence

    Hi!

    In the attached file, column D has the classification for stocks. I would like to write a formula and break up the first, second and third string as shown in columns F, H, J highlighted in yellow. What is giving me trouble is the second " | " which I can't seem to select properly. Any help would be much appreciated.

    Thank you.
    Attached Files Attached Files

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,822

    Re: Extracting the first, middle and last words in a string / sentence

    You can use this formulae in the cells stated:

    E2: =IF(COUNTIF(D2,"*|*"),LEFT(D2,FIND("|",D2)-2),"")

    H2: =IF(COUNTIF(D2,"*|*"),MID(D2,FIND("|",D2)+2,FIND("|",D2,FIND("|",D2)+1)-2-FIND("|",D2)),"")

    J2: =RIGHT(D2,3)

    Then copy down as required.

    Hope this helps.

    Pete

  3. #3
    Registered User
    Join Date
    05-16-2020
    Location
    Lima
    MS-Off Ver
    2019
    Posts
    20

    Re: Extracting the first, middle and last words in a string / sentence

    Thank you so much! it works well.

    You used the "*|*" to tell excel to look for the second term?

  4. #4
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,822

    Re: Extracting the first, middle and last words in a string / sentence

    Not quite - that just detects if there is a pipe character in the text. If not, it returns a blank to that cell.

    Pete

  5. #5
    Registered User
    Join Date
    05-16-2020
    Location
    Lima
    MS-Off Ver
    2019
    Posts
    20

    Re: Extracting the first, middle and last words in a string / sentence

    Do you know why the countif returns a 1 when there are clearly two | in the cell?

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    81,515

    Re: Extracting the first, middle and last words in a string / sentence

    Is one of them right at the start or right at the end of the entire string?
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  7. #7
    Registered User
    Join Date
    05-16-2020
    Location
    Lima
    MS-Off Ver
    2019
    Posts
    20

    Re: Extracting the first, middle and last words in a string / sentence

    This is the entire cell: Technology | Semiconductor Equipment & Materials | Netherlands.
    I'm not sure why he/she is using the asterisk in countif(D4.Find"*|*")

  8. #8
    Registered User
    Join Date
    05-16-2020
    Location
    Lima
    MS-Off Ver
    2019
    Posts
    20

    Re: Extracting the first, middle and last words in a string / sentence

    Hi there!
    J2 - only works if the last initials are 3 letters. There are multiple countries in my list of stocks, e.g (Netherlands). The formula = Left(D4, 3), only pulls "nds". How can I modify it so it pulls "Netherlands".

    Please let me know if I need to upload the spreadsheet again.
    Thank you!
    Last edited by smart_beta; 07-05-2021 at 03:46 AM.

  9. #9
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2406
    Posts
    44,218

    Re: Extracting the first, middle and last words in a string / sentence

    Some countries have multiple-word names. The Netherlands being one of them. Are there multiple-word country names in your dataset?

    It would help if you uploaded a REPRESENTATIVE dataset, instead of one that just looks a bit like the real situation.
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU

  10. #10
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,369

    Re: Extracting the first, middle and last words in a string / sentence

    In J2

    =TRIM(MID(D2,FIND("/",SUBSTITUTE(D2," ","/",LEN(D2)-LEN(SUBSTITUTE(D2," ","")))),30))

    in the last entries there are no delimiters ("|") so what is the result: all blank? If so, above needs to be changed to ....

    =IF(COUNTIF(D2,"*|*"),TRIM(MID(D2,FIND("/",SUBSTITUTE(D2," ","/",LEN(D2)-LEN(SUBSTITUTE(D2," ","")))),30)),"")

    See post #9
    Last edited by JohnTopley; 07-05-2021 at 04:51 AM.

  11. #11
    Registered User
    Join Date
    05-16-2020
    Location
    Lima
    MS-Off Ver
    2019
    Posts
    20

    Re: Extracting the first, middle and last words in a string / sentence

    it works well!

    Nice little trick there finding the number of blank spaces and swapping the last space with a "/" to count the instances. Then finding the position of the / to extract the MID.

    Thank you for teaching me some cool tricks!

    Much appreciated!

  12. #12
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,369

    Re: Extracting the first, middle and last words in a string / sentence

    This is better as it caters for "New Zealand"!

    =IF(COUNTIF(D2,"*|*"),TRIM(MID(D2,FIND("/",SUBSTITUTE(D2,"|","/",LEN(D2)-LEN(SUBSTITUTE(D2,"|",""))))+1,30)),"")

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Delete words in a sentence in a string
    By learner2020 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-11-2020, 02:17 AM
  2. Cross reference letter case: Beginning of sentence and middle of sentence
    By caswell1000 in forum Word Formatting & General
    Replies: 4
    Last Post: 02-07-2019, 05:25 PM
  3. Extracting Middle Name Of A String
    By zanshin777 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-17-2015, 10:36 AM
  4. [SOLVED] Extracting varying length text from the middle of a string
    By tm314159 in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 11-20-2014, 07:23 PM
  5. Extracting words from a string
    By janagan in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-31-2013, 08:40 AM
  6. Extracting Specific Words from string
    By janagan in forum Excel General
    Replies: 3
    Last Post: 08-23-2013, 12:57 AM
  7. [SOLVED] Extracting two words from a string
    By HM001 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 04-14-2013, 07:15 AM

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