+ Reply to Thread
Results 1 to 14 of 14

Find / Extract UK Postcode Using VBA

  1. #1
    Registered User
    Join Date
    01-16-2014
    Location
    London, England
    MS-Off Ver
    Office 365
    Posts
    25

    Find / Extract UK Postcode Using VBA

    Hi All,

    I have some code where I loop through and extract text from a series of PDF's. Within this are some addresses; however these contain postcodes for a number of addresses, which are enclosed by brackets. I want to clean this up and remove the brackets which surround these postcodes within my string. It would also be useful for other subsequent actions to be able to identify the postcodes within the string. The postcode is often surrounded by other text, in the middle of the string. Also, there could be other brackets included within the string around other text, which I do not want to remove (hence, simply locating and removing all brackets is not an option).

    A UK Postcode is in one of the following six combinations of letters and numbers (and spaces): A1 1AA / A11 1AA / AA1 1AA / AA11 1AA / A1A 1AA / AA1A 1AA. I'm sure therefore that there must be a means of identifying and extracting this from a string. I thought perhaps the sequence to follow here would be to first check whether an alpha-numerical combination exists such as those referenced, and if so identify the start and end character location within that string. I could then extract that and also check whether the preceding and subsequent characters were brackets and if so remove these.

    I have seen from searching online that there are formulas which can be used within a workbook for extracting a postcode. When testing however, these seem to be failing for me if the postcode isn't at the very end of the string within the cell.

    One thing I did find (shared elsewhere in response to a similar query on another forum) was as below. I wasn't quite able to wrap my head around this however, therefore using and changing this to suit my specific requirements has been unsuccessful. This does seem to correctly identify whether a UK postcode exists within a cell; however this only returns a True or False statement within the cell with the UDF (i.e. rather than the location of the postcode within the cell). Also, given that my addresses are part of a string within VBA rather than within a cell, I imagine I could only make this work with the use of helper cells (i.e. by inserting the string into a cell, running the UDF in another cell, and then grabbing the output of that) - which if need be, then fine; however my preference would be to have this actioned within the VBA code without creating a UDF and running that from within a cell or cells (if that's possible).

    Please Login or Register  to view this content.
    If anyone has any pointers here, that would be greatly appreciated.

    Cheers - Skins

  2. #2
    Forum Expert
    Join Date
    10-10-2016
    Location
    Sheffield
    MS-Off Ver
    365 and rarely 2016
    Posts
    3,212

    Re: Find / Extract UK Postcode Using VBA

    You really have to include an anonymised sample of say 10-20 lines for someone to offer you a solution.

    Questions
    Are the brackets unique to search string, so could we search between them eg not other brackets appear in the string
    What do you want your output to exactly look like
    Do you need vba or are you open to a formula

    Follow the yellow bar and attach a sample workbook, then someone will be better able to help you

  3. #3
    Registered User
    Join Date
    01-16-2014
    Location
    London, England
    MS-Off Ver
    Office 365
    Posts
    25

    Re: Find / Extract UK Postcode Using VBA

    Thanks davsth. I'm not too sure how much use the code will be, as I have yet to incorporate anything for the postcode identification. Also, this is just a separate code at this stage for the PDF information extract which I am working on before I move this into the master code for looping through all docs. Anyway, code as per the attached.

    Essentially, AOPropName is the variable string which will contain the address and postcode.

    In response to your queries - as mentioned in the original post, there can be other brackets within the string, so we can't use these to search against. The output would remain as per the existing string, just without the brackets surrounding the postcode. Also, I may wish to have a separate output just for the postcode extracted from the string. Also as mentioned, I would prefer VBA; however if this needed to be a formula, then I could make that work (i.e. by sending the string to a cell, running the formula in an adjacent cell, and then returning that cell value back to the VBA / assigning it as a variable etc.).

    Thanks
    Attached Files Attached Files

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,809

    Re: Find / Extract UK Postcode Using VBA

    Could there be something in parenthesis BEFORE the post code? Your sample is one string - NOT ENOUGH. You mentioned possibly having a post code without parenthesis - we need a sample that covers all possible eventualities.

    Try again, please.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,809

    Re: Find / Extract UK Postcode Using VBA

    This formula will work on the sample you've provided:

    =SUBSTITUTE(SUBSTITUTE(A1,TRIM(MID(A1,FIND("(",A1),10)),"|"),"|",SUBSTITUTE(MID(TRIM(MID(A1,FIND("(",A1),10)),2,9),")",""))

    AliGW on MS365 Insider (Windows) 32 bit
    A
    B
    1
    30 St Mary Axe, London (EC3A 8BF) United Kingdom (Second Address). 30 St Mary Axe, London EC3A 8BF United Kingdom (Second Address).
    2
    30 St Mary Axe, London (EC3 8BF) United Kingdom (Second Address). 30 St Mary Axe, London EC3 8BF United Kingdom (Second Address).
    Sheet: Sheet1

  6. #6
    Registered User
    Join Date
    01-16-2014
    Location
    London, England
    MS-Off Ver
    Office 365
    Posts
    25

    Re: Find / Extract UK Postcode Using VBA

    This will search various property title registers to extract addresses. Examples of the data that this might return are as follows:

    10 King Street, Manchester (M2 6AQ) United Kingdom
    2 High St, Harborne, Birmingham (B17 9NE) and 7 High St, Harborne, (Birmingham) B17 9NT
    6 Queens Rd, Liverpool (L6 2BZ) and Land adjacent to 6 Queens Rd

    *Apologies, just update as this was a bad example as it didn't contain other parentheses in the string aside from those around the postcode.

    Thanks
    Last edited by Skins11; 11-01-2021 at 06:36 AM.

  7. #7
    Registered User
    Join Date
    01-16-2014
    Location
    London, England
    MS-Off Ver
    Office 365
    Posts
    25

    Re: Find / Extract UK Postcode Using VBA

    Thanks Ali. This doesn't seem to search for the Postcode format however, therefore this would not catch other eventualities (i.e. something else within parentheses prior to the postcode). I would ideally like for this to specifically search for / identify a postcode (rather than searching for the brackets).

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,809

    Re: Find / Extract UK Postcode Using VBA

    My formula works for these, too:

    AliGW on MS365 Insider (Windows) 32 bit
    A
    B
    1
    30 St Mary Axe, London (EC3A 8BF) United Kingdom (Second Address). 30 St Mary Axe, London EC3A 8BF United Kingdom (Second Address).
    2
    30 St Mary Axe, London (EC3 8BF) United Kingdom (Second Address). 30 St Mary Axe, London EC3 8BF United Kingdom (Second Address).
    3
    10 King Street, Manchester (M2 6AQ) United Kingdom 10 King Street, Manchester M2 6AQ United Kingdom
    4
    2 High St, Harborne, Birmingham (B17 9NE) and 7 High St, Harborne, (Birmingham) B17 9NT 2 High St, Harborne, Birmingham B17 9NE and 7 High St, Harborne, (Birmingham) B17 9NT
    5
    6 Queens Rd, Liverpool (L6 2BZ) and Land adjacent to 6 Queens Rd 6 Queens Rd, Liverpool L6 2BZ and Land adjacent to 6 Queens Rd
    Sheet: Sheet1

  9. #9
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,809

    Re: Find / Extract UK Postcode Using VBA

    ... therefore this would not catch other eventualities (i.e. something else within parentheses prior to the postcode).
    ... which is why you should have provided a properly representative sample!

    Sorry - I don't have time for shifting goalposts, so I'm out.

    Don't expect your helpers to fill the gaps in your provision of data!!!

  10. #10
    Registered User
    Join Date
    01-16-2014
    Location
    London, England
    MS-Off Ver
    Office 365
    Posts
    25

    Re: Find / Extract UK Postcode Using VBA

    Hi Ali,

    Appreciate you looking at this anyway. Just for info, there's no shifting goalposts here. It's the reason why I just set out what I was looking for, and didn't attach a workbook in the first instance (as I don't at this stage have a long list of representative data). I only attached the workbook after davsth suggested I did so. The original post made clear that I was looking to find the location of the postcode, not the parentheses.

    Thanks all the same however, and appreciate you taking the time looking at this.

  11. #11
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,973

    Re: Find / Extract UK Postcode Using VBA

    This UDF should pluck out the postcode from the original text assuming that there is one postcode present.

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    01-16-2014
    Location
    London, England
    MS-Off Ver
    Office 365
    Posts
    25

    Re: Find / Extract UK Postcode Using VBA

    Hi ByteMarks,

    Thanks a lot for this. Seems to work really well on a small sample that I've tested it against. I've also managed to put together a formula which uses this and checks for and removes any parentheses from directly before or after the postcode where these exist. This is as below (where the raw data / address is in cell A1).

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    The formula a tad messy mind. Also, this requires me to first write my string variable (the address) into a cell, then run this formula in another cell, then assign the value of that cell back to another string variable in the VBA code. This is of course doable. I am however just trying to figure out how to apply this directly to my variable string value within the VBA code to prevent the need for the "helper" cells and my formula above. I think this should be possible. If I can figure it out, I will share it back here. If you had any suggestions on that front also, they would be greatly received.

    Once again, much appreciated!

    Skins

    P.S. - For info, where in the formula above it is written "RPLC", this should actually be "REPLACE" - for some reason, wouldn't let me post (the forum firewall was preventing me from doing so, and upon a little research it seems that the forum blocks code containing "REPLACE"). Seems a bit odd, but I'm sure there's a reason for it.
    Last edited by AliGW; 11-01-2021 at 06:34 PM. Reason: PLEASE don't quote unnecessarily!

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

    Re: Find / Extract UK Postcode Using VBA

    It's been 8 years already, so not sure the pattern is already outdated...
    Format cell to recognised UK postcode

    Please Login or Register  to view this content.

  14. #14
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,973

    Re: Find / Extract UK Postcode Using VBA

    I've also managed to put together a formula which uses this and checks for and removes any parentheses from directly before or after the postcode where these exist
    It might be easier to put a loop in the function to replace all occurrences of a postcode with brackets, with just the postcode. Then you can run the function as a UDF or as part of another procedure.

    Please Login or Register  to view this content.

+ 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. Replies: 9
    Last Post: 12-05-2020, 01:40 PM
  2. [SOLVED] Postcode check and return of value in column title and cell adjacent to postcode
    By BigTP in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-26-2018, 05:04 AM
  3. [SOLVED] Extract first or first two letters from postcode
    By gilgil2 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 06-09-2014, 03:13 PM
  4. extract postcode number into new column
    By mathanraj76 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-14-2013, 04:32 AM
  5. Extract postcode after last comma in text string
    By TheRobsterUK in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-15-2013, 09:35 AM
  6. [SOLVED] Help needed trimming full postcode address to postcode sector.
    By Mikey7346 in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 08-10-2012, 05:11 PM
  7. Extract Postcode from free test and sort by it
    By uchidadada in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-03-2010, 10:18 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