Extracting UK post codes

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi Derek ,

Could you provide us the sample of your data and what you want to extract within it so that other people understand and provide you the better solution.

Thanks
 
Upvote 0
How can I extract UK post codes from a non uniform text string into a separate row
Some questions...

1) Will there be only one text string being processed or more than one?

2) What cell or cells will the text string be in?

3) By "separate row", do you mean the row immediately under the one the text string is on?

4) Will there be only one UK post code in the text string or could there more than one?

5) If more than one, where do they go... in separate cells or the same cells?
 
Upvote 0
Derek,
Welcome to Excel.
I think you may be struggling with this one. However, there are some clever folk around on this forum and someone might just come up with something.
I would imagine it would have to be utilising regular expressions.

Here is some post code info that might help someone take up the challenge.

Post codes are two letter /number strings separated by a single space.

Formats.....

AA9A 9AA -- e.g.EC1A 1BB
A9A 9AA -- W1A 1HQ
A9 9AA -- M1 1AA
A99 9AA -- B33 8TH
AA9 9AA -- CR2 6XH
AA99 9AA -- DN55 1PT


  • The letters QVX are not used in the first position.
  • The letters IJZ are not used in the second position.
  • The only letters to appear in the third position are ABCDEFGHJKPSTUW when the structure starts with A9A.
  • The only letters to appear in the fourth position are ABEHMNPRVWXY when the structure starts with AA9A.
  • The final two letters do not use the letters CIKMOV, so as not to resemble digits or each other when hand-written.

Good luck with this one. ** Edit Rick's awake so you might have a chance;)
 
Last edited:
Upvote 0
Hi

Please see

4 examples below extracted from a database in Excel . What I need to do is extract the postcodes highlighted in yellow. Thank you for your response . Derek

Church Farm House, 6 Church Farm Lane, Sidlesham, Chichester, PO20 7RE - F/C Tank Model Major, Flat Top Blowdown Vessel, Sample Cooler, Mini Duplex Water Softener, Delivery & Installation



Supplied 2003 -Installed 41 Market Place, Reading, Berks RG1 2DE Branch Ref: 6524

Skid Assembly with 2 boilers + ancillaries Installed @ Holywell Hospital, 60 Steeple Road, Antrim
BT41 2RJ

Unit 7A Chaucer Business Park, Watery Lane, Kemsing, Sevenoaks, TN15 6PW - Ancillaries Blowdown Vessel, Model "Standard" S/S F&C Tank - Customer collected

 
Upvote 0
Insert the below into a new module

It worked for all 4 examples, then type =Postcode(cell reference with post code)



Original source: Extract postcode from text string


<Code>

Function POSTCODE(ByVal InpStr As String) As String

Dim w As String
Dim j As Long
Dim Ptrn1
Dim Ptrn2 As String

x = Split(Replace(InpStr, ",", " "), " ")

Ptrn1 = Array("[A-Z][0-9]", "[A-Z][0-9][0-9]", "[A-Z][A-Z][0-9]", "[A-Z][A-Z][0-9][0-9]", _
"[A-Z][0-9][A-Z]", "[A-Z][A-Z][0-9][A-Z]")

Ptrn2 = "[0-9]*" '"[0-9][A-Z][A-Z]"

On Error Resume Next
For i = 0 To UBound(x)
w = x(i)
For j = LBound(Ptrn1) To UBound(Ptrn1)
If Len(w) Then
If w Like Ptrn1(j) And x(i + 1) Like Ptrn2 Then
If Err.Number <> 0 Then
Err.Clear
If w Like Ptrn1(j) & Ptrn2 Then
POSTCODE = w: Exit Function
End If
Else
POSTCODE = w & Space(1) & x(i + 1)
Exit Function
End If
ElseIf w Like Ptrn1(j) Then
POSTCODE = w: Exit Function
End If
End If
Next
Next
End Function

</Code>
Hi

Please see

4 examples below extracted from a database in Excel . What I need to do is extract the postcodes highlighted in yellow. Thank you for your response . Derek

Church Farm House, 6 Church Farm Lane, Sidlesham, Chichester, PO20 7RE - F/C Tank Model Major, Flat Top Blowdown Vessel, Sample Cooler, Mini Duplex Water Softener, Delivery & Installation



Supplied 2003 -Installed 41 Market Place, Reading, Berks RG1 2DE Branch Ref: 6524

Skid Assembly with 2 boilers + ancillaries Installed @ Holywell Hospital, 60 Steeple Road, Antrim
BT41 2RJ

Unit 7A Chaucer Business Park, Watery Lane, Kemsing, Sevenoaks, TN15 6PW - Ancillaries Blowdown Vessel, Model "Standard" S/S F&C Tank - Customer collected
 
Upvote 0
Hi Rick

Thanks or the reply

Examples of 4 separate text strings


Church Farm House, 6 Church Farm Lane, Sidlesham, Chichester, PO20 7RE - F/C Tank Model Major, Flat Top Blowdown Vessel, Sample Cooler, Mini Duplex Water Softener, Delivery & Installation

Supplied 2003 -Installed 41 Market Place, Reading, Berks RG1 2DE Branch Ref: 6524

Skid Assembly with 2 boilers + ancillaries Installed @ Holywell Hospital, 60 Steeple Road, Antrim
BT41 2RJ

Unit 7A Chaucer Business Park, Watery Lane, Kemsing, Sevenoaks, TN15 6PW - Ancillaries Blowdown Vessel, Model "Standard" S/S F&C Tank - Customer collected



) Will there be only one text string being processed or more than one? There are around 1000 to process

2) What cell or cells will the text string be in? I will create a separate column

3) By "separate row", do you mean the row immediately under the one the text string is on? I did mean column

4) Will there be only one UK post code in the text string or could there more than one? There will be only one in each string and each cell

5) If more than one, where do they go... in separate cells or the same cells? Into separate cell in a column headed post codes

 
Upvote 0
According to online sources, apparently UK Post Codes can take one of the following formats...

Code:
Format     Example
------     -------
AN NAA     M1 1AA
ANN NAA    M60 1NW
AAN NAA    CR2 6XH
ANA NAA    W1A 1HQ
AANA NAA   EC1A 1BB
AANN NAA   DN55 1PT

where "A" represents an upper case letter and # represents a digit. The following UDF (user defined function) will retrieve any of these formats from the text passed to it in its argument...

Code:
Function UKPostCode(S As String) As String
  Dim X As Long
  For X = 1 To Len(S)
    If Mid(S, X, 6) Like "[A-Z]# #[A-Z][A-Z]*" Then
      UKPostCode = Mid(S, X, 6)
      Exit For
    ElseIf Mid(S, X, 7) Like "[A-Z]## #[A-Z][A-Z]*" Or _
           Mid(S, X, 7) Like "[A-Z][A-Z]# #[A-Z][A-Z]*" Or _
           Mid(S, X, 7) Like "[[A-Z]#[A-Z] #[A-Z][A-Z]*" Then
      UKPostCode = Mid(S, X, 7)
      Exit For
    ElseIf Mid(S, X, 8) Like "[A-Z][A-Z]#[A-Z] #[A-Z][A-Z]*" Or _
           Mid(S, X, 8) Like "[A-Z][A-Z]## #[A-Z][A-Z]*" Then
      UKPostCode = Mid(S, X, 8)
      Exit For
    End If
  Next
End Function

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use UKPostCode just like it was a built-in Excel function. For example,

=UKPostCode(A2)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
Here is some post code info that might help someone take up the challenge.

Post codes are two letter /number strings separated by a single space.

Formats.....

AA9A 9AA -- e.g.EC1A 1BB
A9A 9AA -- W1A 1HQ
A9 9AA -- M1 1AA
A99 9AA -- B33 8TH
AA9 9AA -- CR2 6XH
AA99 9AA -- DN55 1PT


  • The letters QVX are not used in the first position.
  • The letters IJZ are not used in the second position.
  • The only letters to appear in the third position are ABCDEFGHJKPSTUW when the structure starts with A9A.
  • The only letters to appear in the fourth position are ABEHMNPRVWXY when the structure starts with AA9A.
  • The final two letters do not use the letters CIKMOV, so as not to resemble digits or each other when hand-written.

Good luck with this one. ** Edit Rick's awake so you might have a chance;)

Ha-ha! Okay, to recap, after you posted this, I posted code (see Message #8) that found the patterns without regard to the letters being allowed in the positions they occur in, but adding that functionality is not too hard. Several years ago I posted a UK Post Code validator UDF, so just adding it to the module and modifying my code from Message #8 to test any patterns found should eliminate any false matches that might occur from some inadvertent combination of numbers and letters. Personally, I don't think this extra test is needed given the structure of the text samples the OP provided, but you did put the question out there, so for completeness sake, here is my code, modified to test the validity of any post code it finds using the ValidatePostCode function (this can be used as a stand-alone UDF by the way) I have included with it....

Code:
Function UKPostCode(S As String) As String
  Dim X As Long
  For X = 1 To Len(S)
    If Mid(S, X, 6) Like "[A-Z]# #[A-Z][A-Z]*" Then
      If ValidatePostCode(Mid(S, X, 6)) Then
        UKPostCode = Mid(S, X, 6)
        Exit For
      End If
    ElseIf Mid(S, X, 7) Like "[A-Z]## #[A-Z][A-Z]*" Or _
           Mid(S, X, 7) Like "[A-Z][A-Z]# #[A-Z][A-Z]*" Or _
           Mid(S, X, 7) Like "[[A-Z]#[A-Z] #[A-Z][A-Z]*" Then
      If ValidatePostCode(Mid(S, X, 7)) Then
        UKPostCode = Mid(S, X, 7)
        Exit For
      End If
    ElseIf Mid(S, X, 8) Like "[A-Z][A-Z]#[A-Z] #[A-Z][A-Z]*" Or _
           Mid(S, X, 8) Like "[A-Z][A-Z]## #[A-Z][A-Z]*" Then
      If ValidatePostCode(Mid(S, X, 8)) Then
        UKPostCode = Mid(S, X, 8)
        Exit For
      End If
    End If
  Next
End Function

 Function ValidatePostCode(ByVal PostCode As String) As Boolean
  Dim Parts() As String
  PostCode = UCase$(PostCode & " ")
  Parts = Split(PostCode)
  ValidatePostCode = (PostCode = "GIR 0AA " Or PostCode = "SAN TA1 " Or _
              (Parts(1) Like "#[ABD-HJLNP-UW-Z][ABD-HJLNP-UW-Z]" And _
              (Parts(0) Like "[A-PR-UWYZ]#" Or _
               Parts(0) Like "[A-PR-UWYZ]#[0-9A-HJKSTUW]" Or _
               Parts(0) Like "[A-PR-UWYZ][A-HK-Y]#" Or _
               Parts(0) Like "[A-PR-UWYZ][A-HK-Y]#[0-9ABEHMNPRVWXY]")))
End Function

If the OP implement my UDF from Message #8, all he has to do is replace that UDF with the code above... nothing has to be changed on the worksheet itself as I kept the UDF's name that the OP needs to call the same; that is, on the worksheet, the UDF to use is UKPostCode (and that function will automatically call the ValidatePostCode function when it needs to).
 
Upvote 0
Nice one Rick!

It,s easy when you know how.
Thank you, that has opened my eyes to the wider possibilities of using Like.
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,734
Members
449,094
Latest member
dsharae57

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top