+ Reply to Thread
Results 1 to 4 of 4

Extract string from cell with different lenghts

  1. #1
    Registered User
    Join Date
    01-04-2017
    Location
    Dublin, Ireland
    MS-Off Ver
    Office 365
    Posts
    2

    Extract string from cell with different lenghts

    Hi, everyone!

    I am trying to automatically extract the name of clients from a string and am getting stuck in the logics... I have tried a different combination of LEFT, RIGHT, FIND and MID.

    Here are two examples of cells:
    Z-BR-Par Perfeito CPL RTT BR S5-
    OMGS-Carefree Curitiba CPL BR RTT

    The expected result of the first one is Z-BR and the second one is OMGS. So, as you can see, some names contain a dash, what prevents me from using is to extract the string.

    I tried something like this but it did not keep the string as needed.
    =LEFT(C1,(FIND(" ",C1)-((FIND(" ",C1)-(FIND("-",C1))+1))))

    My idea was to count until the space (" ") and subtract the numbers of characters from that space to the first dash (-) to the left. I am failing to count this second step.

    I might be overcomplicating, so any suggestion is greatly appreciated!

  2. #2
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2024
    Posts
    6,175

    Re: Extract string from cell with different lenghts

    Try this ...

    =LEFT(C1,FIND("|",SUBSTITUTE(C1,"-","|",LEN(LEFT(C1,FIND(" ",C1)))-LEN(SUBSTITUTE(LEFT(C1,FIND(" ",C1)),"-",""))))-1)

  3. #3
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Extract string from cell with different lenghts

    For above 2, you only need =Left(C1,4) for both patterns.

    If it's going to be always up to 2nd "-" position or to 1st... you can do something like.
    =IFERROR(LEFT(C1,FIND("~",SUBSTITUTE(C1,"-","~",2))-1),LEFT(C1,FIND("-",C1)-1))

    Or more robust formula. To find last occurrence of "-" before first " ", and extract text to that location -1.
    =LEFT(C1,FIND("~",SUBSTITUTE(C1,"-","~",LEN(LEFT(C1,FIND(" ",C1)))-LEN(SUBSTITUTE(LEFT(C1,FIND(" ",C1)),"-",""))))-1)

    Explanation:
    =LEFT(C1,FIND(" ",C1)) will extract string up to first " ".
    Ex. Z-BR-Par

    From there use
    =LEN("Z-BR-Par")-LEN(SUBSTITUTE("Z-BR-Par","-","")) to count # of "-" that occurs from above result.
    Ex. 2

    Then...
    =FIND("~",SUBSTITUTE("Original String","-","~",2)) will substitute out last occurrence of "-" with "~" before first " " and find it's location in original string.
    Ex. 5

    Finally
    =LEFT(C1,5-1) = Z-BR

    NOTE: Same method as Phuocam's, noticed it while typing explanation
    Last edited by CK76; 01-04-2017 at 08:12 PM. Reason: Shortened IfError formula.

  4. #4
    Registered User
    Join Date
    01-04-2017
    Location
    Dublin, Ireland
    MS-Off Ver
    Office 365
    Posts
    2

    Re: Extract string from cell with different lenghts

    Hello Phuocam and CK76.

    Thank you very much for the help. It worked perfectly. I really appreciate he detailed explanation! It will certainly help to build the logic in my head for next situations.

    I had tried using SUBSTITUTE before but I was placing in the wrong place.

    Thanks and cheers!

+ 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] Extract text from cell with shifting lenghts
    By Sinep D in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-18-2015, 12:46 PM
  2. [SOLVED] Extract text from string in cell
    By VBA FTW in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-26-2013, 05:08 PM
  3. VBA Code for cutting character lenghts into another cell
    By ziemann82 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-01-2013, 08:54 AM
  4. Extract string from a cell
    By scottt725 in forum Excel General
    Replies: 3
    Last Post: 04-10-2012, 10:37 PM
  5. Extract number from cell as string
    By Tey in forum Excel General
    Replies: 4
    Last Post: 04-22-2010, 03:30 AM
  6. Extract string from cell
    By greavesy in forum Excel General
    Replies: 14
    Last Post: 08-20-2009, 10:03 PM
  7. Splitting contents of a cell - Diffirent Lenghts
    By CobraLAD in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-02-2009, 07:33 AM

Tags for this Thread

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