+ Reply to Thread
Results 1 to 27 of 27

Extracting UK postcodes from string of text to import into Microsoft Autoroute.

Hybrid View

  1. #1
    Registered User
    Join Date
    10-06-2012
    Location
    BRISTOL UK
    MS-Off Ver
    Excel
    Posts
    6

    Post Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    Hi, I'm new to this so apologies if this is the wrong place to ask this question.

    I get data emailed to me that is just a string of text, comma separated I think, that I need to extract the UK 5, 6 or 7 digit postcode from.

    An example of the text can be seen below:

    %CA145DX15505382054963814826,%0WS13QE15505382055020814826,%0HU91TQ15505382069227814826,%0WS13QE15505382117853814826,%00L10AB15505382117853814826,

    or sometimes it comes like this:

    %CA145DX15504002468914814826
    %0WS13QE15504002468914814826
    %0HU91TQ15504002468914814826
    %0WS13QE15504002468914814826
    %00L10AB15504002468914814826

    Ideally, i would like to set up a template, that had some special macros or VBA or whatever you call it, that would ignore the leading % and 1 or 2 zeros and the rest of the numbers after the postcode and just extract the 5, 6 or 7 digit postcode so I can import them into Microsoft Autoroute to use the locations.

    Is this possible?

    The postcodes are highlighted in bold to make it easier to see them.

    Thanks in advance for your help.

    Al.

  2. #2
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    what code have you got so far?
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  3. #3
    Registered User
    Join Date
    10-06-2012
    Location
    BRISTOL UK
    MS-Off Ver
    Excel
    Posts
    6

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    You've got me there...!

    I have searched round the net for an answer, seen that other people have had a similar problem, with postcode data mixed up with other stuff.

    I HAVE NO IDEA how to write code, writing a simple formula in a spreadsheet is the limit of my abilities, but I am willing to learn.

    Thanks for your quick response.

    Microsoft Autoroute is called Streets and Trips in the US.

    Thanks,

    Al.

  4. #4
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    See if this UDF helps.

    In a standard module
    Function GetPostCodes(rng As Range)
        Dim strTemp As String, strResult As String
        Dim n As Long, r As Long
        Dim arrSplit As Variant
        
        strTemp = Replace(rng, "%", "")
        arrSplit = Split(strTemp, ",")
        
        For r = 0 To UBound(arrSplit)
            strTemp = arrSplit(r)
            For n = Len(strTemp) To 1 Step -1
                Select Case Mid(strTemp, n, 1)
                    Case 0 To 9
                        strTemp = Left(strTemp, n - 1)
                    Case Else
                        Exit For
                End Select
            Next
            For n = 1 To Len(strTemp)
                Select Case Left(strTemp, 1)
                    Case 0 To 9
                        strTemp = Right(strTemp, Len(strTemp) - 1)
                    Case Else
                        Exit For
                End Select
            Next
            strResult = strResult & strTemp & ", "
        Next
        If strResult = "" Then
            GetPostCodes = ""
        Else
            GetPostCodes = Left(strResult, Len(strResult) - 2)
        End If
    End Function
    Use in Excel like so ...
    In say B2 Drag Down
    Formula: copy to clipboard
    =GetPostCodes(A2)


    Then in C2, Drag Across then Down
    Formula: copy to clipboard
    =TRIM(MID(SUBSTITUTE(", "&$B2,", ",REPT(" ",255)),255*COLUMNS($C:C),255))


    See this workbook
    Attached Files Attached Files
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  5. #5
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    Formula: copy to clipboard
    =MID(LOOKUP("zzz",INDEX(LEFT(A1,ROW($1:$8)),0)),MIN(SEARCH({"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"},LOOKUP("zzz",INDEX(LEFT(A1,ROW($1:$8)),0))&"ABCDEFGHIJKLMNOPQRSTUVWXYZ")),LEN(LOOKUP("zzz",INDEX(LEFT(A1,ROW($1:$8)),0))))

    or maybe just
    Formula: copy to clipboard
    =MID(MID(A1,2,7),MIN(SEARCH({"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"},MID(A1,2,7)&"ABCDEFGHIJKLMNOPQRSTUVWXYZ")),LEN(MID(A1,2,7)))
    Attached Files Attached Files
    Last edited by martindwilson; 10-06-2012 at 07:50 AM.

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,731

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    This seems to work
    Sub test()
        Dim myPtn As String, a, i As Long, m As Object
        myPtn = "([A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))" & _
                "|[0-9][A-HJKS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2}|(GIRr 0A{2})|(SAN ?TA1)|(BFPO ?" & _
                "(C\/O )?[0-9]{1,4})|((ASCN|B{2}ND|[BFS]IQ{2}|PCRN|STHL|TDCU|TKCAa) {0,1}1Z{2}))"
        With Range("a1").CurrentRegion.Resize(, 2)
            a = .Value
            With CreateObject("VBScript.RegExp")
                .Global = True
                .IgnoreCase = True
                .Pattern = myPtn
                For i = 1 To UBound(a, 1)
                    a(i, 2) = Empty
                    For Each m In .Execute(a(i, 1))
                        a(i, 2) = a(i, 2) & ", " & m.Value
                    Next
                    If Len(a(i, 2)) Then a(i, 2) = Mid$(a(i, 2), 3)
                Next
            End With
            .Value = a
        End With
    End Sub
    Attached Files Attached Files

  7. #7
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    Another formula approach
    Formula: copy to clipboard
    =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LEFT(A2,FIND(LOOKUP(99^99,1*RIGHT(TRIM(A2),COLUMN($1:$1)))/10^5,A2)-1),"%00",""),"%0",""),"%","")


    Use Text to Colums to split cells with comma seperated strings.
    Then Copy the result > Paste Special > Transpose (Check the Transpose box)

    Refer the formula to the first cell, then drag down.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    10-06-2012
    Location
    BRISTOL UK
    MS-Off Ver
    Excel
    Posts
    6

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    Thanks all, will give this a go when I get back home.

    Not sure what I do with the formula, but if I open the attached .xlsm files, and paste some more data, should that work?

    Al.

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,731

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    Quote Originally Posted by MULTIDROP View Post
    Not sure what I do with the formula, but if I open the attached .xlsm files, and paste some more data, should that work?

    Al.
    Should work.

    Note: the code only extracts valid post code(s).
    Last edited by jindon; 10-06-2012 at 05:21 PM.

  10. #10
    Banned User!
    Join Date
    10-14-2006
    Posts
    1,211

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    Quote Originally Posted by Marcol View Post
    Another formula approach
    Formula: copy to clipboard
    =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LEFT(A2,FIND(LOOKUP(99^99,1*RIGHT(TRIM(A2),COLUMN($1:$1)))/10^5,A2)-1),"%00",""),"%0",""),"%","")


    Use Text to Colums to split cells with comma seperated strings.
    Then Copy the result > Paste Special > Transpose (Check the Transpose box)

    Refer the formula to the first cell, then drag down.
    Shorter version:

    =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LEFT(A2,8),"%00",),"%0",),"%",)

  11. #11
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    i like that,,,,,,,,,,,,,,

  12. #12
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    @ Teethless mama

    Your formula will not work for all cases.
    UK Post codes can have from 5 to 7 characters, the last three are seperated from the rest by a space.

    Consider these possibillities
    String	                        Result	Actual Code
    %M11AA15504002468914814826	M11AA	M1 1AA
    %0WS13QE15504002468914814826	WS13QE	WS1 3QE
    %0HU91TQ15504002468914814826	HU91TQ	HU9 1TQ
    %0WS13QE15504002468914814826	WS13QE	WS1 3QE
    %00EC1A1BB15504002468914814826	EC1A1BB	EC1A 1BB
    See this workbook
    Attached Files Attached Files

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,731

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    Updated version (output)
    Attached Files Attached Files

  14. #14
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    using teethleess mama idea
    Formula: copy to clipboard
    =left(substitute(substitute(substitute(left(a1,8),"%00",),"%0",),"%",),len(substitute(substitute(substitute(left(a1,8),"%00",),"%0",),"%",))-3)&" "&right(substitute(substitute(substitute(left(a1,8),"%00",),"%0",),"%",),3)

  15. #15
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    @ Martin

    Your formula still seems to be restricted to 6 character codes
    As I said in Post #12
    UK Post codes can have from 5 to 7 characters, the last three are seperated from the rest by a space.
    My suggestion in Post#7 handles all possible UK codes, provided there aren't invalid codes in the OPs data strings.
    Formula: copy to clipboard
    =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LEFT(A2,FIND(LOOKUP(99^99,1*RIGHT(TRIM(A2),COLUMN($1:$1)))/10^5,A2)-1),"%00",""),"%0",""),"%","")


    With the formula in B2, this can then be used in C2 to partially validate the code and return the properly formatted code.
    Formula: copy to clipboard
    =IF(AND(LEN(B2)>=5,LEN(B2)<=7,ISNUMBER(MID(B2,LEN(B2)-2,1)*1)),LEFT(B2,LEN(B2)-3)&" "&RIGHT(B2,3),"Invalid Code")

  16. #16
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    marcol i think the string always takes that into consideration hence the leading zeros for padding
    so you wouldnt get
    %00EC1A1BB15504002468914814826
    but instead
    %EC1A1BB15504002468914814826
    and
    %M11AA15504002468914814826
    would be
    %00M11AA15504002468914814826

  17. #17
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    @ Martin
    i think the string always takes that into consideration hence the leading zeros for padding
    Ahh ... I didn't think of that!

    So if the code correctly needs the space then maybe
    Formula: copy to clipboard
    =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LEFT(A2,5),"%00",""),"%0",""),"%","")&" "&MID(A2,6,3)


    If the space isn't required then Teethless mammas' suggestion suffices
    Formula: copy to clipboard
    =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LEFT(A2,8),"%00",),"%0",),"%",)
    Attached Files Attached Files
    Last edited by Marcol; 10-07-2012 at 09:07 AM. Reason: Added Attachment

  18. #18
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    now we're getting there!

  19. #19
    Registered User
    Join Date
    10-06-2012
    Location
    BRISTOL UK
    MS-Off Ver
    Excel
    Posts
    6

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    Hi again, and thanks to you all for taking time over working this out.

    If it helps, when I import data into Microsoft Autoroute to map the locations, it doesn't matter if the postcodes have the gap between the first 2, 3 or 4 digits and the last 3 digits, it recognises them as UK postcodes as long as they are in the same cell in a spreadsheet.

    Ideally the "extraction" would leave the extracted postcodes in a new column so i can cut and paste that into a new blank spreadsheet and import that data into MS Autoroute. It lets me know if any of the postcodes are not recognised as valid UK ones at that point, and I can make a note and validate them manually.
    (this only really occurs with new build properties that aren't in the Autoroute data base yet).

    Could someone, really simply, step by step talk me through how I get your solutions to work, set up a new spreadsheet, which cells to paste the formula in etc, and can they be used with Open Office as well?

    Thanks once again.

    Al.

  20. #20
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    as long as you put say
    %CA145DX15504002468914814826
    %0WS13QE15504002468914814826
    %0HU91TQ15504002468914814826
    %0WS13QE15504002468914814826
    %00L10AB15504002468914814826
    into column a starting say a1 down
    then in b1 put
    =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LEFT(A1,8),"%00",),"%0",),"%",)
    fill the formula down

    if they are in the format
    %CA145DX15505382054963814826,%0WS13QE15505382055020814826,%0HU91TQ15505382069227814826,%0WS13QE15505382117853814826,%00L10AB15505382117853814826
    copy the lot into word
    find comma ,
    replace with ^p
    replace all
    this will put them one below each other
    then use the formula as above
    (note 1 in open office writer find/replace - in more options -allow regular expressions -then find comma replace with \n )
    (note 2 if you write the formula in open office calc then it's
    =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LEFT(A1;8);"%00";);"%0";);"%";)
    because semi colons are used instead of commas however when you open in excel they are auto converted and vica-versa)

  21. #21
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,731

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    Just paste the data in col.A of Sheet1, it will output the result in sheet2 when button is clicked.
    Attached Files Attached Files

  22. #22
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    Could someone, really simply, step by step talk me through how I get your solutions to work, set up a new spreadsheet, which cells to paste the formula in etc, and can they be used with Open Office as well?
    nope that code will not run in open office,the formulas will.

  23. #23
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,731

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    Open office?

    Forget about my codes.

  24. #24
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    The solutions to your problem have already been posted to this thread.

    You say in Post #1
    An example of the text can be seen below:

    %CA145DX15505382054963814826,%0WS13QE15505382055020814826,%0HU91TQ15505382069227814826,%0WS13QE15505382117853814826,%00L10AB15505382117853814826,

    or sometimes it comes like this:

    %CA145DX15504002468914814826
    %0WS13QE15504002468914814826
    %0HU91TQ15504002468914814826
    %0WS13QE15504002468914814826
    %00L10AB15504002468914814826
    How about you post a typical workbook showing exactly what you import to Excel?

    That way someone can perhaps summarise this thread and return your workbook as a template.

  25. #25
    Registered User
    Join Date
    10-06-2012
    Location
    BRISTOL UK
    MS-Off Ver
    Excel
    Posts
    6

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    OK, here is an example, I just copy and paste the text from an email into Excel 2003.

    Hope this gives you some idea, there can be up to 100 postcodes to extract in one go.

    Thanks once again for your time and effort.

    Al.
    Attached Files Attached Files

  26. #26
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.

    i think i gave you the answer in post #20, please read all the responses and come back if there is something you don't understand

  27. #27
    Registered User
    Join Date
    10-06-2012
    Location
    BRISTOL UK
    MS-Off Ver
    Excel
    Posts
    6

    Re: Extracting UK postcodes from string of text to import into Microsoft Autoroute.



    Yes, thanks, your first formula in post 20 works just great.

    In Excel and Open Office.

    Thanks for your help

    Al.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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