Finding A UK Post Code in a Random String

welshraz

New Member
Joined
Apr 29, 2016
Messages
42
Office Version
  1. 365
Platform
  1. Windows
I have a set of very annoying data that I need to pull the UK postcodes from. They look like this:

WOODBURY";"WOODBURY";"EX5 1EE";"(SDU)";"Parent Shell";"false";"0";"0";"0";"";"Other";"";"Safe";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"false";"false";"false";"false";"";"";"";"50.679902";"-3.391585"

EXETER";"WOODBURY SALTERTON";"EX5 1EL";"(SDU)";"Commercial";"false";"1";"0";"0";"";"Other";"";"Contract";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"false";"false";"false";"false";"";"";"";"50.697388";"-3.386321"

BARNSTAPLE";"BARNSTAPLE";"EX32 9LP";"SDU";"Residential";"true";"0";"1";"0";"D";"Other";"Future";"Unknown";"NORTHMOOR";"";"";"";"";";"";"false";"false";"false";"false";"";"1313zobYAAS";"view";"51.070795";"-4.033579"

The post code is not always in the same position, and not always in the same column of data. I was using a combo of TEXTBEFORE and TEXTAFTER, using the space as the delimiter, but some town names have more that one word in their name, so that is a bust.

Any help greatly appreciated.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
If it's always after the 2nd ; how about
Fluff.xlsm
AB
1
2WOODBURY";"WOODBURY";"EX5 1EE";"(SDU)";"Parent Shell";"false";"0";"0";"0";"";"Other";"";"Safe";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"false";"false";"false";"false";"";"";"";"50.679902";"-3.391585"EX5 1EE
3EXETER";"WOODBURY SALTERTON";"EX5 1EL";"(SDU)";"Commercial";"false";"1";"0";"0";"";"Other";"";"Contract";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"false";"false";"false";"false";"";"";"";"50.697388";"-3.386321"EX5 1EL
4BARNSTAPLE";"BARNSTAPLE";"EX32 9LP";"SDU";"Residential";"true";"0";"1";"0";"D";"Other";"Future";"Unknown";"NORTHMOOR";"";"";"";"";";"";"false";"false";"false";"false";"";"1313zobYAAS";"view";"51.070795";"-4.033579"EX32 9LP
Sheet6
Cell Formulas
RangeFormula
B2:B4B2=CHOOSECOLS(TEXTSPLIT(SUBSTITUTE(A2,CHAR(34),""),";"),3)
 
Upvote 0
Here is a UDF I have used in the past, I have amended it to work with your data. It should find the postcode wherever it may be. This could also be done with REGEX as well I would imagine, but I already had this code saved in my code snippet folder.
VBA Code:
Function UKPostcode(InpStr As String)
    Dim w As String, Ptrn2 As String
    Dim j As Long, i As Long
    Dim Ptrn1 As Variant, x As Variant
 
    x = Split(Replace(Replace(InpStr, """", ""), " ", ";"), ";")
   
    '                L    N        L    N    N        L    L    N        L    L    N    N
    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]")
    Ptrn2 = "[0-9]*" ' Number & something
   
    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 UKPostcode = w: Exit Function
                    Else
                        UKPostcode = w & Space(1) & x(i + 1): Exit Function
                    End If
                ElseIf w Like Ptrn1(j) Then
                    UKPostcode = w: Exit Function
                End If
            End If
        Next j
    Next i
    On Error GoTo 0
End Function

Used in the sheet as below:
Book1
AB
1WOODBURY";"WOODBURY";"EX5 1EE";"(SDU)";"Parent Shell";"false";"0";"0";"0";"";"Other";"";"Safe";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"false";"false";"false";"false";"";"";"";"50.679902";"-3.391585"EX5 1EE
2EXETER";"WOODBURY SALTERTON";"EX5 1EL";"(SDU)";"Commercial";"false";"1";"0";"0";"";"Other";"";"Contract";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"false";"false";"false";"false";"";"";"";"50.697388";"-3.386321"EX5 1EL
3BARNSTAPLE";"BARNSTAPLE";"EX32 9LP";"SDU";"Residential";"true";"0";"1";"0";"D";"Other";"Future";"Unknown";"NORTHMOOR";"";"";"";"";";"";"false";"false";"false";"false";"";"1313zobYAAS";"view";"51.070795";"-4.033579"EX32 9LP
Sheet1
Cell Formulas
RangeFormula
B1:B3B1=UKPostcode(A1)
 
Upvote 1
Solution
Here is a UDF I have used in the past, I have amended it to work with your data. It should find the postcode wherever it may be. This could also be done with REGEX as well I would imagine, but I already had this code saved in my code snippet folder.
VBA Code:
Function UKPostcode(InpStr As String)
    Dim w As String, Ptrn2 As String
    Dim j As Long, i As Long
    Dim Ptrn1 As Variant, x As Variant
 
    x = Split(Replace(Replace(InpStr, """", ""), " ", ";"), ";")
  
    '                L    N        L    N    N        L    L    N        L    L    N    N
    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]")
    Ptrn2 = "[0-9]*" ' Number & something
  
    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 UKPostcode = w: Exit Function
                    Else
                        UKPostcode = w & Space(1) & x(i + 1): Exit Function
                    End If
                ElseIf w Like Ptrn1(j) Then
                    UKPostcode = w: Exit Function
                End If
            End If
        Next j
    Next i
    On Error GoTo 0
End Function

Used in the sheet as below:
Book1
AB
1WOODBURY";"WOODBURY";"EX5 1EE";"(SDU)";"Parent Shell";"false";"0";"0";"0";"";"Other";"";"Safe";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"false";"false";"false";"false";"";"";"";"50.679902";"-3.391585"EX5 1EE
2EXETER";"WOODBURY SALTERTON";"EX5 1EL";"(SDU)";"Commercial";"false";"1";"0";"0";"";"Other";"";"Contract";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"";"false";"false";"false";"false";"";"";"";"50.697388";"-3.386321"EX5 1EL
3BARNSTAPLE";"BARNSTAPLE";"EX32 9LP";"SDU";"Residential";"true";"0";"1";"0";"D";"Other";"Future";"Unknown";"NORTHMOOR";"";"";"";"";";"";"false";"false";"false";"false";"";"1313zobYAAS";"view";"51.070795";"-4.033579"EX32 9LP
Sheet1
Cell Formulas
RangeFormula
B1:B3B1=UKPostcode(A1)
This is *chefs kiss*!! Thank you so much
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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