+ Reply to Thread
Results 1 to 18 of 18

Split before Zip Code

  1. #1
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Split before Zip Code

    Hi guys

    I have a application where I extract a address from folder names.
    The address is not always how it should be acording to our rules for creating case folders.

    So I am trying to split it and delete the parts that should not be there.

    This is the folderPath "X:\IN4MO\TILBUDSSAGER FORSIKRINGSAFDELINGEN\Frederik d. II's Allé 335, 8660 Skanderborg"
    Then I extract The address "Frederik d. II's Allé 335, 8660 Skanderborg"
    Then I Replace the comma with Space "Frederik d. II's Allé 335 8660 Skanderborg"
    Then I Replace double space with single space "Frederik d. II's Allé 335 8660 Skanderborg"

    Here is where I am stuck.
    I want to split the string Before the Zip code that is always 4 digit.

    I have made a sample file with the code working up to where to split at Zip code, I have no idear how to do that.

    I hope some one can guide me to a solution.
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: Split before Zip Code

    Try this:

    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,150

    Re: Split before Zip Code

    Another alternative is using "Regular Expressions" in a UDF which should be entered as an array function (i.e. Ctrl+Shift+Enter) to get before and after ZIP code.

    Please Login or Register  to view this content.
    Sample workbook is also attached...
    Attached Files Attached Files
    Last edited by Haluk; 07-19-2020 at 05:15 PM. Reason: typo...

  4. #4
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Split before Zip Code

    Thank you TMS

    It works perfect.

    Now I can go to ed without thinking of this :-)
    Tomorrow I wil analyze your code to understand it and learn :-)

  5. #5
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Split before Zip Code

    Thank you Haluk

    Very nice

    I had looked in regex but it was way past my skils as a vba beginner :-)

    But I can se that it is a prefered solution for finding and extracting patens.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: Split before Zip Code

    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  7. #7
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,150

    Re: Split before Zip Code

    You're welcome...

    Yes, RegEx is an useful tool for this type of problems.
    Last edited by Haluk; 07-19-2020 at 05:24 PM.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: Split before Zip Code

    @Haluk: I love to see Regular Expressions ... I just have no idea how they work . Nice solution and avoids all the intermediate stages.

    The only issue is that the function doesn't extract anything if there is a ZIP code.

  9. #9
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,150

    Re: Split before Zip Code

    @TMS;

    It's nice to see a RegEx fan and thx for the rep.

    Actually I was aware about the fact you mentioned ... But its somewhat late in Turkey now. Tomorow I'll look at it again.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: Split before Zip Code

    I would think ...

    Please Login or Register  to view this content.

  11. #11
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Split before Zip Code

    I believe this non-RegExp macro will also work...
    Please Login or Register  to view this content.
    SIDE NOTE: I used to write Regular Expression scripts on a UNIX mini-computer back in the 1990's, but haven't touched it since. I do remember saying back then that Regular Expressions is one of few languages where you could write something that you could not figure out what it did when reading it back a half-hour after having written it.
    Last edited by Rick Rothstein; 07-20-2020 at 03:40 AM. Reason: Modified the code slightly

  12. #12
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,150

    Re: Split before Zip Code

    @TMS;

    Something like this may work:

    Please Login or Register  to view this content.
    Last edited by Haluk; 07-20-2020 at 03:52 AM.

  13. #13
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Split before Zip Code

    I am not sure why a UDF solution is being pushed instead of a macro solution. I do not see the advantage of loading up Columns C and D with array-entered UDF formulas... it seems having a macro place constants in those cells would be more efficient overall. That is why I posted the non-RegExp macro that I did in Message #11. However, if an array-entered UDF turns out to be what the OP ends up wanting, here is such non-RegExp for the OP to consider...
    Please Login or Register  to view this content.

  14. #14
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,150

    Re: Split before Zip Code

    @Rick Rothstein;

    UDF solution is not being pushed and no one is being forced to use it..... as you can see in my first message (#3) I offered just an alternative solution with RegEx.

    The rest of the messages continued with advantages and disadvantages of RegEx.

    Note: The RegEx function can also be compiled as a "Sub" procedure as you know.
    Last edited by Haluk; 07-20-2020 at 04:08 AM.

  15. #15
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Split before Zip Code

    Sorry, "pushed" was a bad choice of words. I still think a macro solution would be more efficient overall. You should consider recasting your UDF into a macro in case the OP wants to go with a RegExp solution rather than a non-RegExp solution just so he has the choice.

  16. #16
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,150

    Re: Split before Zip Code

    So; if the OP wants to use the RegEx solution provided in message #12 in a Sub procedure, one alternative is;

    Please Login or Register  to view this content.
    Last edited by Haluk; 07-20-2020 at 05:15 AM.

  17. #17
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Split before Zip Code

    Thanks Rick
    Nice work, It is perfekt.

  18. #18
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,211

    Re: Split before Zip Code

    Formula solution
    F2
    =TRIM(SUBSTITUTE(RIGHT(SUBSTITUTE(B2,"\",REPT(" ",99)),99),G2,))

    G2
    =IFNA(TRIM(MID(SUBSTITUTE(" "&B2," ",REPT(" ",200)),MATCH(TRUE,INDEX(--TEXT(FILTERXML("<b><a>"&SUBSTITUTE(B2," ","</a><a>")&"</a></b>","//a"),"0;;;\0")>999,),)*200,6^6)),"")

+ 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] Code to split
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-16-2016, 12:16 PM
  2. need vba code to split rows to closed wb shets depends company code met with sht name
    By julielara in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-18-2016, 12:44 PM
  3. Need Macro code to split data split in 7 sheets based on variable rows in column A
    By Alija_21 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-31-2015, 08:03 AM
  4. [SOLVED] How to Split VBA code in New lines
    By naveenmarapaka in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-08-2014, 02:19 AM
  5. [SOLVED] VB Code to split data
    By Oszie in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 11-27-2013, 08:37 AM
  6. how to convert excel split code to word split code
    By gsrikanth in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2012, 07:56 AM
  7. Split code in more than one page ?
    By SpookiePower in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-16-2006, 06:25 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