UK post code re-format

tmac999

New Member
Joined
Jun 17, 2010
Messages
6
I need a macro to re-format UK postcode data to the form:
Format Example
AN NAA M1 1AA
ANN NAA M60 1NW
AAN NAA CR2 6XH
AANN NAA DN55 1PT
ANA NAA W1A 1HQ
AANA NAA EC1A 1BB

The issues with the data are double spaces, triple or no spaces between the outward code and inward code and capitalisation. Also some entries have spaces where there should be no spaces e.g M 1 1AA from the first example above.

Any help greatly received.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi,

If you want it to correctly space the postcode, try a UDF:
Code:
Option Explicit

Function FormatPostcode(ByVal PostCode As String) As Variant
'AN NAA M1 1AA
'ANN NAA M60 1NW
'AAN NAA CR2 6XH
'AANN NAA DN55 1PT
'ANA NAA W1A 1HQ
'AANA NAA EC1A 1BB
Dim iPtr As Integer
Dim sInput As String, sChar As String, sMask As String

For iPtr = 1 To Len(PostCode)
    sChar = UCase$(Mid$(PostCode, iPtr, 1))
    If IsNumeric(sChar) Then
        sMask = sMask & "9"
        sInput = sInput & sChar
    ElseIf sChar<> LCase$(sChar) Then
        sMask = sMask & "A"
        sInput = sInput & sChar
    End If
Next iPtr

Select Case sMask
Case "A99AA"            'AN NAA M1 1AA
    FormatPostcode = Left$(sInput, 2) & " " & Right$(sInput, 3)
    
Case "A999AA", "A9A9AA"            'ANN NAA M60 1NW or ANA NAA W1A 1HQ
    FormatPostcode = Left$(sInput, 3) & " " & Right$(sInput, 3)
    
Case "AA99AA"           'AAN NAA CR2 6XH
    FormatPostcode = Left$(sInput, 3) & " " & Right$(sInput, 3)
    
Case "AA999AA", "AA9A9AA"          'AANN NAA DN55 1PT or AANA NAA EC1A 1BB
    FormatPostcode = Left$(sInput, 4) & " " & Right$(sInput, 3)
    
Case Else
    FormatPostcode = CVErr(xlErrNA)
End Select
End Function

For example:
Excel Workbook
AB
1M11aaM1 1AA
2m691nwM69 1NW
3cr26xhCR2 6XH
4dn551ptDN55 1PT
5w1a1hqW1A 1HQ
6ec1a1bbEC1A 1BB
Sheet1
Excel 2003
Cell Formulas
RangeFormula
B1=FormatPostcode(A1)
B2=FormatPostcode(A2)
B3=FormatPostcode(A3)
B4=FormatPostcode(A4)
B5=FormatPostcode(A5)
B6=FormatPostcode(A6)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
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