Vlookup based on contents of a cell

geordie_ben

Board Regular
Joined
Jul 27, 2009
Messages
122
I've gotten a list of post codes and their counties.

This is how the data is presented:

[TABLE="class: wikitable sortable jquery-tablesorter"]
<thead>[TR]
[TH="class: headerSort, width: %2221%22, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=EAECF0]#EAECF0[/URL] , align: center"]Postcode area[/TH]
[TH="class: headerSort, width: %2221%22, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=EAECF0]#EAECF0[/URL] , align: center"]Postcode districts[/TH]
[TH="class: headerSort, width: %2221%22, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=EAECF0]#EAECF0[/URL] , align: center"]Post town[/TH]
[TH="class: headerSort, width: %2221%22, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=EAECF0]#EAECF0[/URL] , align: center"]Former postal county[/TH]
[/TR]
</thead><tbody>[TR]
[TD]AB[/TD]
[TD]AB10, AB11, AB12, AB15, AB16,
AB21, AB22, AB23, AB24, AB25,
AB99non-geo[/TD]
[TD]Aberdeen[/TD]
[TD]<small style="font-size: 11.9px;">(Aberdeenshire)</small>[/TD]
[/TR]
</tbody>[/TABLE]
I need to do some type of lookup (I think), where the first part of a full post code being AB22 would show Aberdeenshire

Is there a type of formula I could use to do this?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi, you could extract the postcode area from a full postcode with a formula like this:


Excel 2013/2016
AB
1Full postcodePostcode area
2AB22 0AAAB
Sheet1
Cell Formulas
RangeFormula
B2=LEFT(A2,AGGREGATE(15,6,FIND({0,1,2,3,4,5,6,7,8,9},A2),1)-1)


You could then do a standard VLOOKUP() to the get post town or county.
 
Upvote 0
Hi

Thanks for the super quick reply

The above was a snippet of every post code in the UK

Would you formula still work?

I can't do the lookup on "AB" in the above example as some Postcode Areas have more than County inside them
 
Upvote 0
The above was a snippet of every post code in the UK

Hi, a slightly bigger snippet, along with a few examples of the inputs and expected results be helpful. But pending that - here's another stab.


Excel 2013/2016
ABCDEFG
1Postcode areaPostcode districtsPost townFormer postal countyFull postcodetown
2ABAB10, AB11, AB12, AB15, AB16, AB21, AB22, AB23, AB24, AB25, AB99non-geoAberdeen(Aberdeenshire)AB22 0AAAberdeen
Sheet1
Cell Formulas
RangeFormula
G2=VLOOKUP("*"&LEFT(F2,FIND(" ",F2&" ")-1)&"*",B:C,2,0)
 
Upvote 0
Hi

The list of information is from here - https://en.wikipedia.org/wiki/List_of_postcode_districts_in_the_United_Kingdom

This is a decent snippet:

[TABLE="class: wikitable sortable jquery-tablesorter"]
<tbody>[TR]
[TD]B[/TD]
[TD]B1, B2, B3, B4, B5, B6, B7, B8, B9,
B10, B11, B12, B13, B14, B15, B16, B17, B18, B19,
B20, B21, B23, B24, B25, B26, B27, B28, B29,
B30, B31, B32, B33, B34, B35, B36, B37, B38,
B40, B42, B43, B44, B45, B46, B47, B48,
B99non-geo[/TD]
[TD]Birmingham[/TD]
[TD]<small style="font-size: 11.9px;">(West Midlands)</small>[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]B49,
B50[/TD]
[TD]Alcester[/TD]
[TD]Warwickshire[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]B60, B61[/TD]
[TD]Bromsgrove[/TD]
[TD]Worcestershire[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]B62, B63[/TD]
[TD]Halesowen[/TD]
[TD]West Midlands[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]B64[/TD]
[TD]Cradley Heath[/TD]
[TD]West Midlands[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]B65[/TD]
[TD]Rowley Regis[/TD]
[TD]West Midlands[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]B66, B67[/TD]
[TD]Smethwick[/TD]
[TD]West Midlands[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]B68, B69[/TD]
[TD]OLDBURY[/TD]
[TD]West Midlands[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]B70, B71[/TD]
[TD]West Bromwich[/TD]
[TD]West Midlands[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]B72, B73, B74, B75, B76[/TD]
[TD]Sutton Coldfield[/TD]
[TD]West Midlands[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]B77, B78, B79[/TD]
[TD]TAMWORTH[/TD]
[TD]Staffordshire[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]B80[/TD]
[TD]STUDLEY[/TD]
[TD]Warwickshire[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]B90, B91, B92, B93, B94[/TD]
[TD]Solihull[/TD]
[TD]West Midlands[/TD]
[/TR]
</tbody>[/TABLE]
I have a list of customer post codes, and I need to establish which County they're in, which is Column D in the above example

Example:

Postcode is B75 1AZ

I've done a text to columns, delimited on space to get the first part of the Post Code (I've manually checked them and they're all good - phew!)

The formula would then bring back the County as West Midlands

Hopefully this helps
 
Upvote 0
I've done a text to columns, delimited on space to get the first part of the Post Code

Hi, you can try this:


Excel 2013/2016
ABCDEFG
1BB1, B2, B3, B4, B5, B6, B7, B8, B9,B10, B11, B12, B13, B14, B15, B16, B17, B18, B19,B20, B21, B23, B24, B25, B26, B27, B28, B29,B30, B31, B32, B33, B34, B35, B36, B37, B38,B40, B42, B43, B44, B45, B46, B47, B48,B99non-geoBirmingham(West Midlands)
2BB49,B50AlcesterWarwickshireB75West Midlands
3BB60, B61BromsgroveWorcestershire
4BB62, B63HalesowenWest Midlands
5BB64Cradley HeathWest Midlands
6BB65Rowley RegisWest Midlands
7BB66, B67SmethwickWest Midlands
8BB68, B69OLDBURYWest Midlands
9BB70, B71West BromwichWest Midlands
10BB72, B73, B74, B75, B76Sutton ColdfieldWest Midlands
11BB77, B78, B79TAMWORTHStaffordshire
12BB80STUDLEYWarwickshire
13BB90, B91, B92, B93, B94SolihullWest Midlands
Sheet1
Cell Formulas
RangeFormula
G2=LOOKUP(2,1/(ISNUMBER(SEARCH(F2&",",$B$1:$B$999&","))),$D$1:$D$999)
 
Upvote 0
Out of interest, why did you change the formula?

Hi, I realised that the VLOOKUP() with the wildcards is not robust. It would match B2 with B22 if B22 happened to occur in a row before B2 for example.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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