+ Reply to Thread
Results 1 to 20 of 20

How to split a string based on a sub string

  1. #1
    Registered User
    Join Date
    11-24-2014
    Location
    ausie
    MS-Off Ver
    2013
    Posts
    8

    How to split a string based on a sub string

    Good day all,

    I have a string say 'the code of the safe is x/o 123456 can you open it'

    My challenge is to split the string into 3 parts
    'the code of the safe is '
    'x/o 123456'
    ' can you open it'

    The middle part could also be 'X\o 147856783' or x \ o 12756', this slight variations but always in the form of 'x' followed by as '\' followed by a 'o' and then a number string.

    Any idea on how to do this?

    thanks all

  2. #2
    Forum Expert
    Join Date
    09-11-2014
    Location
    Washington, DC
    MS-Off Ver
    2016
    Posts
    1,907

    Re: How to split a string based on a sub string

    Does the phrase always begin with "The code of the safe is" and dows it always end with "Can you open it"?
    Spread the love, add to the Rep

    "None of us are as smart as all of us."

  3. #3
    Registered User
    Join Date
    11-24-2014
    Location
    ausie
    MS-Off Ver
    2013
    Posts
    8

    Re: How to split a string based on a sub string

    Important point you made there, no it will be different in most cases, the only constant if I can put it that way will be the sequence of 'x' + '\' + 'o'.

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,957

    Re: How to split a string based on a sub string

    Can you provide a list of all possible variations on x/o please?

    And, what determines the end of teh 2nd phrase?
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  5. #5
    Forum Expert
    Join Date
    09-11-2014
    Location
    Washington, DC
    MS-Off Ver
    2016
    Posts
    1,907

    Re: How to split a string based on a sub string

    Is it a forwardslash or a backslash?

  6. #6
    Forum Contributor
    Join Date
    04-02-2012
    Location
    Ohio
    MS-Off Ver
    Excel 2010
    Posts
    452

    Re: How to split a string based on a sub string

    The text to columns feature under the data tab should work, you will possibly have to use the CONCATENATE function afterwards to get it perfectly organized.

  7. #7
    Registered User
    Join Date
    11-24-2014
    Location
    ausie
    MS-Off Ver
    2013
    Posts
    8

    Re: How to split a string based on a sub string

    If not case sensitive
    x \o
    x\o
    x\ o
    x \ o

    If case sensitive then include below
    X \o
    x \O
    X \ O
    X\O
    X \ O
    X \O
    X\ O

    Thanks

  8. #8
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,957

    Re: How to split a string based on a sub string

    Your example in post 1 is x/o...so is it / or \ ?

  9. #9
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: How to split a string based on a sub string

    Maybe this

    Just follow this order

    in B1

    =LEFT(A1,SEARCH("x",A1)-1)

    In D1

    =TRIM(MID(SUBSTITUTE(SUBSTITUTE(A1,LEFT(A1,SEARCH("x",A1)-1),"")," ",REPT(" ",255),2),255,255))

    in C1

    =SUBSTITUTE(SUBSTITUTE(A1,LEFT(A1,SEARCH("x",A1)-1),""),D1,"")

    Row\Col
    A
    B
    C
    D
    1
    the code of the safe is x/o 123456 can you open it the code of the safe is x/o 123456 can you open it
    If you like my answer please click on * Add Reputation
    Don't forget to mark threads as "Solved" if your problem has been resolved

    "Nothing is so firmly believed as what we least know."
    --Michel de Montaigne

  10. #10
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: How to split a string based on a sub string

    Hi dippa and welcome to the forum,

    See if
    =MID(A1,FIND(UPPER("x"),UPPER(A1)),FIND(" ",A1,FIND(UPPER("o"),UPPER(A1),FIND("\",A1))+2)-FIND(UPPER("x"),UPPER(A1)))
    works for you. See the attached example.
    Attached Files Attached Files
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  11. #11
    Registered User
    Join Date
    11-24-2014
    Location
    ausie
    MS-Off Ver
    2013
    Posts
    8

    Re: How to split a string based on a sub string

    Hi Mate '/'

    thanks

  12. #12
    Registered User
    Join Date
    11-24-2014
    Location
    ausie
    MS-Off Ver
    2013
    Posts
    8

    Re: How to split a string based on a sub string

    Hi Mate '/'

    thanks

  13. #13
    Registered User
    Join Date
    11-24-2014
    Location
    ausie
    MS-Off Ver
    2013
    Posts
    8

    Re: How to split a string based on a sub string

    thanks guys, both work well enough for me to do what I need to do, would still be interested if there are more solutions.

  14. #14
    Registered User
    Join Date
    01-25-2013
    Location
    Fort Belvoir, VA
    MS-Off Ver
    Excel 2013/2007
    Posts
    13

    Re: How to split a string based on a sub string

    Greetings.

    Looks like Marvin and AlKey submitted good answers for you, but personally i like having multiple ways forward in case the situation changes on the spreadsheet side. Try these to see if they work for you:

    Assumptions:
    *I assume your text lies in column A
    *I assume the format is constant: 'the code of the safe is **####### can you open it' with the only variety being the actual combination or the style of the x/o (x\o, X/O or any other format will work fine)
    *I assume you have columns B thru D available for formulas. if you do not, I recommend inserting blank columns there, or referencing you full string on another sheet and doing the formula work there
    *I assume this sheet is not protected beyond your ability to modify. If you do not have rights to change things and add formulas, you will need to do this work in another workbook and add it in somewhere/somehow else




    In column B, top cell adjacent to your data (assumed as B1): =LEFT(A1, FIND("s ",A1))
    In column C same row as previous: =MID(A1,FIND("s ",A1)+2,FIND(" ca",A1)-FIND("s ",A1)-2)
    In column D, same row as previous: =MID(A1,FIND(" ca",A1)+1,LEN(A1))

    This should break that string along the words before and after the blank on either side of the combination substring, there for allowing for subtle changes while retaining function. If you cahnge the wording so the word 'is' and 'can' are not those bookend words, you can swap the new letter out in the formulas and it will still work. As said before, anything inbetween these two bookends will get cut and placed into the middle cell.

    Good luck!

    Mike

  15. #15
    Registered User
    Join Date
    11-24-2014
    Location
    ausie
    MS-Off Ver
    2013
    Posts
    8

    Re: How to split a string based on a sub string

    MarvinP,
    fyi
    "the code x of the safe is x\O 123456 can you open it" Restults in "x of the safe is x\O 123456"
    thanks again

  16. #16
    Registered User
    Join Date
    11-24-2014
    Location
    ausie
    MS-Off Ver
    2013
    Posts
    8

    Re: How to split a string based on a sub string

    I can force my users to enter a constant sting of "w/o", would simplify it a little?

  17. #17
    Registered User
    Join Date
    01-25-2013
    Location
    Fort Belvoir, VA
    MS-Off Ver
    Excel 2013/2007
    Posts
    13

    Re: How to split a string based on a sub string

    I am confused by the 'w/o'...and it can be hard to change users behavoir in excel, especially if they are undertrained. I try to code around their habits.

    With the setup in my post, the only necessary parts are the words is and can. "************is &&&&&&& can **********" will give you:

    *********is

    &&&&&&&&&

    can ********

    in three separate cells.

    Happy Excelling

  18. #18
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: How to split a string based on a sub string

    I think that taking different approach may be somewhat a better option. Instead of extracting the middle part, use formula to extract numbers only and add "x/o " at the beginning. This seems to a more reliable way of doing that and you can avoid any inconsistencies with X/ or X / and so one.

    Try this one

    ="x/o "&LOOKUP(25^25,0+MID(A1,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&1234567890)),ROW($1:$10000)))

  19. #19
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,957

    Re: How to split a string based on a sub string

    miloki, perhaps you missed this part from posts 2 & 3?
    Does the phrase always begin with "The code of the safe is" and dows it always end with "Can you open it"?
    Important point you made there, no it will be different in most cases, the only constant if I can put it that way will be the sequence of 'x' + '\' + 'o'.
    So there is no guarantee that you will be able to find "s "

  20. #20
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: How to split a string based on a sub string

    Here is an easier formula that keys off the "\".

    See if it works better for you.

    =MID(A1,SEARCH("\",A1)-3,SEARCH(" ",A1,SEARCH("\",A1)+5)-SEARCH("\",A1)+3)
    Attached Files Attached Files

+ 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. Generating random number string based on Alphanumeric String
    By ridemeve in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-15-2014, 04:56 PM
  2. Replies: 1
    Last Post: 04-12-2014, 04:03 PM
  3. Advanced Split Function: Need to pull text string in quotes, within a text string
    By Zamboni in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-27-2013, 04:38 PM
  4. [SOLVED] Split string based on character count
    By cedric_dranreb in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-19-2013, 05:14 AM
  5. Split String From A Cell Based On Multiple Criteria
    By ssanjju in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-05-2013, 03:21 PM

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