Consulting

Results 1 to 17 of 17

Thread: Copying part of a Text String of different lengths

  1. #1
    VBAX Contributor
    Joined
    Jun 2008
    Location
    West Midlands
    Posts
    170
    Location

    Copying part of a Text String of different lengths

    Hi guys,

    I wonder if you can help me, I need some code to copy part of a text string in a cell, usually I would simply use a MID formula, however each cell will have different data lengths, and example of it is below;

    "400 (HOMEWARES)/403 (FOOD WRAP & STORAGE)/111 (FOOD STORAGE)/103 (MICROWAVE CONTAINERS)"

    400 (HOMEWARES)/405 (DINING)/101 (TABLEWARE)/111 (MUGS/CUPS)

    550 (CELEBRATIONS)/567 (TABLE & DRINKWARE)/101 (PARTYWARE)/103 (GLASSES)


    In this example I only want to copy the text I have highlighted in Red from each cell, I then want to concatenate all the collected data into one cell.

    Any ideas?

  2. #2
    Like this:

    <takes deeeep breath>

    =LEFT(MID(MID(A1,FIND("/",A1)+1,LEN(A1)),FIND("(",MID(A1,FIND("/",A1)+1,LEN(A1)))+1,LEN(A1)),FIND(")",MID(MID(A1,FIND("/",A1)+1,LEN(A1)),FIND("(",MID(A1,FIND("/",A1)+1,LEN(A1)))+1,LEN(A1)))-1)

    <releases deep breath>
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Fixed it for you Mark.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Quote Originally Posted by Jan Karel Pieterse View Post

    =LEFT(MID(MID(A1,FIND("/",A1)+1,LEN(A1)),FIND("(",MID(A1,FIND("/",A1)+1,LEN(A1)))+1,LEN(A1)),FIND(")",MID(MID(A1,FIND("/",A1)+1,LEN(A1)),FIND("(",MID(A1,FIND("/",A1)+1,LEN(A1)))+1,LEN(A1)))-1)

    <releases deep breath>
    "Only one deep breath"... very impressive. but errr... lets see, 1 left, 6 lens, 6 Mids, 7 Finds and 44 Parenthesis's . Any chance you could explain it in under 100 words?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Quote Originally Posted by Jan Karel Pieterse View Post
    =LEFT(MID(MID(A1,FIND("/",A1)+1,LEN(A1)),FIND("(",MID(A1,FIND("/",A1)+1,LEN(A1)))+1,LEN(A1)),FIND(")",MID(MID(A1,FIND("/",A1)+1,LEN(A1)),FIND("(",MID(A1,FIND("/",A1)+1,LEN(A1)))+1,LEN(A1)))-1)
    I'm guessing the "FIND("/",A1)" finds the first occurrence of the "/", while "(Mid(Find("/",A1)+1 finds the second occurrence of the"/"? and after that I'm lost
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  7. #7
    OK, I was expecting that, just didn't have time to split this into pieces.

    Lets take this example:
    400 (HOMEWARES)/403 (FOOD WRAP & STORAGE)/111 (FOOD STORAGE)/103 (MICROWAVE CONTAINERS)"

    First step: find first / and get all text after that:

    =MID(A1,FIND("/",A1)+1,LEN(A1))

    Result:
    403 (FOOD WRAP & STORAGE)/111 (FOOD STORAGE)/103 (MICROWAVE CONTAINERS)"

    Step 2: find first (, get all text after that. because we need to find it in the string we got in step 1, we repeat that piece of the formula:

    =MID(MID(A1,FIND("/",A1)+1,LEN(A1)),FIND("(",MID(A1,FIND("/",A1)+1,LEN(A1)))+1,LEN(A1))

    Result:

    FOOD WRAP & STORAGE)/111 (FOOD STORAGE)/103 (MICROWAVE CONTAINERS)"

    Now we need the part up to the first ). Again, we repeat the formula from the previous step:

    =LEFT(MID(MID(A1,FIND("/",A1)+1,LEN(A1)),FIND("(",MID(A1,FIND("/",A1)+1,LEN(A1)))+1,LEN(A1)),FIND(")",MID(MID(A1,FIND("/",A1)+1,LEN(A1)),FIND("(",MID(A1,FIND("/",A1)+1,LEN(A1)))+1,LEN(A1)))-1)

    Clear as mud?
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  8. #8
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Absolutely. Yep Clear as Mud,....


    Just one question, are we talking European Mud or Aussie Mud here? European mud is eco friendly, no biomass, ultra low emissions and could be distilled as a rum base whilst aussie mud is something you could bog a low flying duck at 50 paces in.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  9. #9
    Dutch mud of course.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Some less mud:

    =MID(A1;FIND("(";A1;6)+1;FIND(")/1";A1)-FIND("(";A1;6)-1)

    or

    =MID(LEFT(A15;FIND(")/1";A15));FIND("(";A15;6);100)
    Last edited by snb; 01-17-2014 at 09:44 AM.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Another way, that doesn't depend on any structure in the first part

    =MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"(",CHAR(1),2)),FIND(CHAR(1),SUBSTITUTE( A1,")",CHAR(1),2))-FIND(CHAR(1),SUBSTITUTE(A1,"(",CHAR(1),2))+1)
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I think JKP wins for the longest WS function so far this year

    I could never keep that much straight in my head, so that's what drove me to VBA many (many, many, many) years ago.

    Option Explicit
    Sub drv()
        MsgBox SplitOut("400 (HOMEWARES)/403 (FOOD WRAP & STORAGE)/111 (FOOD STORAGE)/103 (MICROWAVE CONTAINERS)")
    End Sub
     
    Function SplitOut(s As String) As String
        Dim sTemp As String
        Dim vSplit As Variant
        Dim iParen As Long
        
        vSplit = Split(s, "/")
        
        sTemp = vSplit(1)
        
        iParen = InStr(sTemp, "(")
        
        SplitOut = Right(sTemp, Len(sTemp) - iParen + 1)
    End Function
    My preference is to be wordy, and not try to write everything into a single statement.

    I figure the computer doesn't care, and I'm the person who will have to try and read it in 6-8 months

    Paul

  13. #13
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    or
    Sub M_snb()
        MsgBox F_snb("400 (HOMEWARES)/403 (FOOD WRAP & STORAGE)/111 (FOOD STORAGE)/103 (MICROWAVE CONTAINERS)")
    End Sub
    
    Function F_snb(c00)
       F_snb = Split(Split(c00, "(")(2), ")")(0)
    End Function

  14. #14
    Hi Paul,

    Thanks. When can I collect my award?
    :-)
    I don't like VBA UDF's much. Even though I agree they may be easier to understand and maintain, they are often cause of havoc in complex files. Slow recalcs and troublesome VBA debugging come to mind.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Jan Karel Pieterse View Post
    Hi Paul,

    Thanks. When can I collect my award?
    :-)
    Whenever you are ready to travel over to the States at your own cost Jan Karel

    Quote Originally Posted by Jan Karel Pieterse View Post
    I don't like VBA UDF's much. Even though I agree they may be easier to understand and maintain, they are often cause of havoc in complex files. Slow recalcs and troublesome VBA debugging come to mind.
    Seconded. I especially hate it when you are f8 debugging and the UDF gets called ... thousands of times ... uugh!
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  16. #16
    a Control+shift+F8 does get you out of that state.

    What's worse is when the UDF does not have any error handling and does cause a runtime error, your code just ends.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  17. #17
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yeah, there is always something you can do, but it still keeps happening.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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