Assign numeric code to data table

sly_sud

New Member
Joined
Jan 22, 2016
Messages
9
I have a table with information in the following manner.

ContinentCountry City
AsiaChinaBeijing
AsiaChinaTianjin
AsiaIndiaBengaluru
AsiaIndiaMumbai
AsiaThailandBangkok
EuropeFranceLyon
EuropeFranceParis
EuropeGermanyBerlin
EuropeGermanyMunich
EuropeUKLondon
EuropeUKManchester
North AmericaCanadaMontreal
North AmericaCanadaToronto
North AmericaMexicoGuadalajara
North AmericaMexicoMexico City
North AmericaUSANew York
North AmericaUSAWashington

<colgroup><col><col><col></colgroup><tbody>
</tbody>

I want to assign numeric code to the cities in this manner.

ContinentCountry CityCode
AsiaChinaBeijing01-01-001
AsiaChinaTianjin01-01-002
AsiaIndiaBengaluru01-02-001
AsiaIndiaMumbai01-02-002
AsiaThailandBangkok01-03-001
EuropeFranceLyon02-01-001
EuropeFranceParis02-01-002
EuropeGermanyBerlin02-02-001
EuropeGermanyMunich02-02-002
EuropeUKLondon02-03-001
EuropeUKManchester02-03-002
North AmericaCanadaMontreal03-01-001
North AmericaCanadaToronto03-01-002
North AmericaMexicoGuadalajara03-02-001
North AmericaMexicoMexico City03-02-002
North AmericaUSANew York03-03-001
North AmericaUSAWashington03-03-002

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>


What formula should I use to create this number code. Please help.

Thanks in advance.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Okay.. with your first table in column A,B,C, type 1 in D2, E2 and F2, now apply below formula in

D3:-
=IF(A3=A2,D2,D2+1)

E3:-
=IF(A3<>A2,1,IF(AND(A3=A2,B3=B2),E2,E2+1))

F3:-
=COUNTIF($B$2:B3,B3)

and drag down.

Now, if you wish you can concatenate these three codes to appear in a single column :)

Regards,
DILIPandey
 
Upvote 0
in D1 put
01-01-001

in D2 put
=TEXT(LEFT(E1,2)+IF(A2=A1,0,1),"00-")&TEXT(IF(A1<>A2,1,MID(E1,4,2)+IF(B2<>B1,1,0)),"00-")&TEXT(IF(OR(A1<>A2,B1<>B2),1,RIGHT(E1,3)+1),"000")
and copy down the column
 
Upvote 0
In D2, formula copy down :

=TEXT(SUMPRODUCT(1/COUNTIF(A$2:A2,A$2:A2)),"00-")&TEXT(SUMPRODUCT((A$2:A2=A2)/COUNTIFS(A$2:A2,A$2:A2,B$2:B2,B$2:B2)),"00-")&TEXT(COUNTIF(B$2:B2,B$2:B2),"000")
 
Upvote 0
Hi

Thank you for the help. Really appreciate it. But the formula you gave isn't working. This is what I am getting as a response

ContinentCountryCity
AsiaChinaBeijing02-01-001
AsiaChinaTianjin01-01-002
AsiaIndiaBengaluru01-02-001
AsiaIndiaMumbai01-01-002
AsiaThailandBangkok01-02-001
EuropeFranceLyon02-01-001
EuropeFranceParis01-01-002
EuropeGermanyBerlin01-02-001
EuropeGermanyMunich01-01-002
EuropeUKLondon01-02-001
EuropeUKManchester01-01-002
North AmericaCanadaMontreal02-01-001
North AmericaCanadaToronto01-01-002
North AmericaMexicoGuadalajara01-02-001
North AmericaMexicoMexico City01-01-002
North AmericaUSANew York01-02-001
North AmericaUSAWashington01-01-002

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>

I used the formula in this manner =TEXT(LEFT($E$1,2)+IF(A2=A1,0,1),"00-")&TEXT(IF(A1<>A2,1,MID($E$1,4,2)+IF(B2<>B1,1,0)),"00-")&TEXT(IF(OR(A1<>A2,B1<>B2),1,RIGHT($E$1,3)+1),"000")

Thanks again.
 
Upvote 0
in D1 put
01-01-001

in D2 put
=TEXT(LEFT(E1,2)+IF(A2=A1,0,1),"00-")&TEXT(IF(A1<>A2,1,MID(E1,4,2)+IF(B2<>B1,1,0)),"00-")&TEXT(IF(OR(A1<>A2,B1<>B2),1,RIGHT(E1,3)+1),"000")
and copy down the column

Modify Special-K99's formula in D1 to meet with the OP desired Output layout

(I think D1 is a typo, and should read as 00-01-001)

1] In D1, enter : 00-01-001

then, >> D1 >> Custom Cell Formatting, enter : 0;0;0;"CODE"

2] D2 copydown formula same as Special-K99's
 
Upvote 0
Hi

I am coming back with a similar query again. Turns out the data list I had was not the final and new additions will keep happening to it. So I need a numeric code as previously, only this time, that data list is unsorted and the list will keep increasing.

The data looks like this, but it won't be sorted alphabetically. And the code at the end is what I need.

Continent Country City Code
Asia China Beijing 01-01-001
Asia China Tianjin 01-01-002
Asia India Bengaluru 01-02-001
Asia India Mumbai 01-02-002
Asia Thailand Bangkok 01-03-001
Europe France Lyon 02-01-001
Europe France Paris 02-01-002
Europe Germany Berlin 02-02-001
Europe Germany Munich 02-02-002
Europe UK London 02-03-001
Europe UK Manchester 02-03-002
North America Canada Montreal 03-01-001
North America Canada Toronto 03-01-002
North America Mexico Guadalajara 03-02-001
North America Mexico Mexico City 03-02-002
North America USA New York 03-03-001
North America USA Washington 03-03-002


Thanks in advance.
 
Upvote 0

Forum statistics

Threads
1,214,605
Messages
6,120,473
Members
448,967
Latest member
visheshkotha

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