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).
If anyone has any pointers here, that would be greatly appreciated.
Cheers - Skins
Bookmarks