UK Postal Code Validation

JuExcel

New Member
Joined
Dec 15, 2009
Messages
3
Hi, I am looking to validate UK Postal Code using MS Excel formula, can anyone help!!! The postal code is should be in the following format:

AN NAA
ANN NAA
AAN NAA
ANA NAA
AANA NAA
AANN NAA

When A is alphabet and N is number. If the condition is incorrect then highlight that particular cell that contains the incorrect postcode.
Many thanks in advance.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Code:
Function ValidPostCode(ByVal PostCode As String) As Boolean
Dim Sections() As String
    PostCode = UCase$(PostCode)
    Sections = Split(PostCode)
    If PostCode = "GIR 0AA" Or PostCode = "SAN TA1" Or _
        (Sections(1) Like "#[A-Z][A-Z]" And _
        (Sections(0) Like "[A-Z]#" Or Sections(0) Like "[A-Z]#[0-9ABCDEFGHJKSTUW]" Or _
         Sections(0) Like "[A-Z][A-Z]#" Or Sections(0) Like "[A-Z][A-Z]#[0-9ABEHMNPRVWXY]")) Then
        ValidPostCode = ((Sections(0) Like "[BEGLMSW]#*" Or _
                          Sections(0) Like "A[BL]#*" Or _
                          Sections(0) Like "B[ABDHLNRST]#*" Or _
                          Sections(0) Like "C[ABFHMORTVW]#*" Or _
                          Sections(0) Like "D[ADEGHLNTY]#*" Or _
                          Sections(0) Like "E[CHNX]#[AMNRVY]" Or _
                          Sections(0) Like "F[KY]#*" Or _
                          Sections(0) Like "G[LU]#*" Or _
                          Sections(0) Like "H[ADGPRSUX]#*" Or _
                          Sections(0) Like "I[GPV]#*" Or _
                          Sections(0) Like "K[ATWY]#*" Or _
                          Sections(0) Like "L[ADELNSU]#*" Or _
                          Sections(0) Like "M[EKL]#*" Or _
                          Sections(0) Like "N[EGNPRW]#*" Or _
                          Sections(0) Like "O[LX]#*" Or _
                          Sections(0) Like "P[AEHLOR]#*" Or _
                          Sections(0) Like "R[GHM]#*" Or _
                          Sections(0) Like "S[AEGKLMNOPRSTWY]#*" Or _
                          Sections(0) Like "T[ADFNQRSW]#*" Or _
                          Sections(0) Like "W[ACDFNRSV]#*" Or _
                          Sections(0) Like "UB#*" Or _
                          Sections(0) Like "YO#*" Or _
                          Sections(0) Like "ZE#*") And _
                          Sections(1) Like "*#[!CIKMOV][!CIKMOV]")
    Else
        ValidPostCode = False
    End If
End Function
 
Upvote 0
Hi, thanks for the reply appreciate it. to be honest I don't Know how to run this code and I am looking for a formula like this:

=AND(LEN(A1)=6,CODE(LEFT(A1))>64,CODE(LEFT(A1))<91,ISNUMBER(--MID(A1,2,1)),MID(A1,3,1)=" ",ISNUMBER(--MID(A1,4,1)),CODE(MID(A1,5,1))>64,CODE(MID(A1,5,1))<91,CODE(RIGHT(A1,1))>64,CODE(RIGHT(A1,1))<91)

this will validate AN NAA and I can change it to do the other validation but can not put them to gether in one formula. can you help? Thanks
 
Upvote 0
The function that Xld has provided goes beyond what you requested in that it performs a complete validation according to existing UK post codes (well, looks that way to me anyhow).

With your workbook that contains the postcodes you want to validate open and visible on your Excel screen, use Alt+F11 to open up the Visual Basic Editor, then use Alt+I+M to insert a standard module in your workbook's Project, then paste in the above function code Xld has provided.

Then, go back to your sheet, Select all your postcodes (eg A2:A1000) and go Format>Conditional Formatting>change to Formula Is and use the formula:

=(1-ValidPostCode(A2))*(A2<>"")

and hit the Format button and select a red pattern. Then click OK to finish.

Any cells failing the test will now be coloured red.
 
Upvote 0
Thanks for the instructions on how to run that function, I have managed to run the function but it fails to highlight the most of the incorrect postcode and highlighted some of the correct postcode.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
As I mentioned earlier to validate AN NAA<o:p></o:p>
I have:<o:p></o:p>
<o:p> </o:p>
=AND(LEN(A1)=6,CODE(LEFT(A1))>64,CODE(LEFT(A1))<91,ISNUMBER(--MID(A1,2,1)),MID(A1,3,1)=" ",ISNUMBER(--MID(A1,4,1)),CODE(MID(A1,5,1))>64,CODE(MID(A1,5,1))<91,CODE(RIGHT(A1,1))>64,CODE(RIGHT(A1,1))<91)<o:p></o:p>
<o:p> </o:p>
And to validate ANN NAA<o:p></o:p>
I have: <o:p></o:p>
<o:p> </o:p>​
=AND(LEN(A1)=7,CODE(LEFT(A1))>64,CODE(LEFT(A1))<91,ISNUMBER(--MID(A1,2,2)),MID(A1,4,1)=" ",ISNUMBER(--MID(A1,5,1)),CODE(MID(A1,6,1))>64,CODE(MID(A1,6,1))<91,CODE(RIGHT(A1,1))>64,CODE(RIGHT(A1,1))<91)
<o:p> </o:p>​
Now I want to join these two formulas together and only give TRUE if the data in cell A1 meets the condition of one of the above formulas. Any help would be appreciated. Many thanks
 
Upvote 0
Hi, thanks for the reply appreciate it. to be honest I don't Know how to run this code and I am looking for a formula like this:

=AND(LEN(A1)=6,CODE(LEFT(A1))>64,CODE(LEFT(A1))<91,ISNUMBER(--MID(A1,2,1)),MID(A1,3,1)=" ",ISNUMBER(--MID(A1,4,1)),CODE(MID(A1,5,1))>64,CODE(MID(A1,5,1))<91,CODE(RIGHT(A1,1))>64,CODE(RIGHT(A1,1))<91)

this will validate AN NAA and I can change it to do the other validation but can not put them to gether in one formula. can you help? Thanks

I wouldn't do that, just plug that function into the VBE and use

=ValidPostCode(A1)

it will return True or False
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
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