Combining formulae for UK postcodes

paulsolar

Well-known Member
Joined
Aug 21, 2013
Messages
691
Office Version
  1. 365
Hi All

I have a method of sorting out typos and in correct entries of UK postcodes, but it takes 4 helper columns to do it.

Below are the formulae I use to do it, the original postcode is in A2,
In B2 I have =TRIM(A2) this is to get rid of any superfluous spaces at the beginning or end of the postcode
In C2 I have =UPPER(B2) this sort out the case as all the letters need to be upper case
In D2 I have =SUBSTITUTE(C2," ","") this removes any spaces in the wrong place
In E2 I have =IF(LEN(D2)-3=2,LEFT(D2,2)&" "&RIGHT(D2,3),IF(LEN(D2)-3=3,LEFT(D2,3)&" "&RIGHT(D2,3),IF(LEN(D2)-3=4,LEFT(D2,4)&" "&RIGHT(D2,3)))) this puts the space in the correct place.

I have tried combing the formulae together but cant seem to get it to work, are any of you formulae experts able to assist me please so that I just need one formula in B2

Cheers

Paul
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi, many thanks for your prompt reply.

Postcodes in the UK always end with NLL proceeded by a space (where N is a number and L is a letter)

The first part of the postcode can vary in length (hence the if statements) and is a combination of Letters and Numbers with quite a few variations.

Here are some examples of correctly formatted post codes, NP10 9AF, W1A 4TT, B12 5QR, L1 9PP

Here are some examples of the postcodes that I receive.. " np1 09aF" , "w1 A4tt ", B1 25qr", "L19pp "

I am not aware of any postcodes where the left part is greater than 4 letters or numbers, but just in case I should probably if added an additional if statement to cover the left part being 5.

I hope this is clear for you

Cheers

Paul
 
Upvote 0
Thanks for the examples. Try this:
Excel Formula:
=UPPER(SUBSTITUTE(TEXTBEFORE(B4,RIGHT(TRIM(B4),3))," ","") & " " & RIGHT(TRIM(B4),3))
 
Upvote 0
Simply combining the logic of your original formula might look like:
Excel Formula:
=LET(pc,SUBSTITUTE(UPPER(TRIM(A2))," ",""),IF(LEN(pc)-3=2,LEFT(pc,2)&" "&RIGHT(pc,3),IF(LEN(pc)-3=3,LEFT(pc,3)&" "&RIGHT(pc,3),IF(LEN(pc)-3=4,LEFT(pc,4)&" "&RIGHT(pc,3)))))
 
Upvote 0
Solution
Thanks Guys thats brilliant

Georgiboy, I would never have considered doing it you way but to be honest it's very clever. I've never used LET, but i will certainly be looking into it!!

Thanks for you help, really appreciate it

Cheers

Paul
 
Upvote 0
I don't know if you tried my formula but I had my reference on B4, you might need to adjust it to A2.
 
Upvote 0
As all UK postcodes have 3 characters after the space, you could use
Excel Formula:
=LET(pc,SUBSTITUTE(UPPER(TRIM(A2))," ",""),REPLACE(pc,LEN(pc)-2,0," "))
 
Upvote 0
As all UK postcodes have 3 characters after the space, you could use
Excel Formula:
=LET(pc,SUBSTITUTE(UPPER(TRIM(A2))," ",""),REPLACE(pc,LEN(pc)-2,0," "))
Hi Fluff, I'll check yours out as well(y)
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,145
Members
452,615
Latest member
bogeys2birdies

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