Home » excel » Excel Generating a formula based sequence based on levels

Excel Generating a formula based sequence based on levels

Posted by: admin May 14, 2020 Leave a comment


What I am trying to achieve is the following

 ID            Name       Level 
 1             ABC           1
 11            ABC           2
 11001         ABC           3
 1100101       ABC           4
 110010101     ABC           5
 11001010101   ABC           6
 11002         ABC           3
 1100201       ABC           4
 110020101     ABC           5
 11002010101   ABC           6
 11002010102   ABC           6
 110020102     ABC           5
 11002010201   ABC           6
 11002010202   ABC           6
 11002010203   ABC           6
 2             ABC           1
 21            ABC           2
 21001         ABC           3
 2100101       ABC           4
 210010101     ABC           5
 210010102     ABC           6
 21002         ABC           3    

I have the following code in excel to generate this code but I m not able to achieve the above, can anyone help me with this, please find below the formula I am using:


Basically I am trying to generate the code in the ID field automatically, let me explain how it works: ID are set using the Levels field for Level 1 its the first digit in the ID field it represents, for Level 2 its the 2nd digit for level 3 its the 3 digits after 2 digits , and afterwards 2 digits for each levels. Furthermore once a level 1 has been assigned it cannot to assigned to another level 1 therefore the second level 1 will be give the next number, its first level 2 will have 1 as number and 2nd level 2 within will give it 2 and the same goes for the following code.

The problem with my code is that it calculates the value fine till the first level 1 and its below code but however its start behaving incorrectly as I go down the codes, it should not print the level code for 4,5,6 and more if I am generating a level 3. these are some of the problems I am facing

Thanks in advance

How to&Answers:

I can do it although I use a helper column. It could all go in one cell but it gets way longer then a formula I would like to see

If you have levels in column A you will calculate a decimal ID in column B and an actual ID in column C

cell B2=1 (since the formula doesn’t work on the first of the series)

other cells in column B use the formula


Entered with Ctrl + Shift + Enter

This formula finds the value of the decimal ID from the last row that has a level less then or equal to itself (indirect part)

the index looks up the Level and adds the right amount to the value found in the first part of the formula.

Column C uses the formula


which just rounds the number up to a integer by finding the number of decimal points (if statement) and then raising 10 to that power and multiplying by the decimal ID.

If you really need to do it in one column you could paste the whole first column everywhere you see B2 in the second formula but that would make the formula totally illegible.

enter image description here



As per the request for more levels I’ve had to use a different technique which deals mainly with text.

Instead of using the arrays built right into the formulas I created a seperate table which makes it a little easier to handle, you could reference this with named ranges, a seperate chart or just put the numbers right in the formula if you want.

enter image description here

For the cells you enter the Level and sigfigs.

Digits kept is sigfigs from the level above minus 1 (M3=L2-1)

Digit Add start is the sigfigs from level above (N3=L2)

Digits add length = sigfigs from level – sigfigs from level above +1 (`O3=L3-L2+1)

For the actual index formula the first row has the value 1 again and then in cell B3 use

=LEFT(B2,INDEX($M$2:$M$12,MATCH(A3,$K$2:$K$12,0))) & MID(B2&"00000",INDEX($N$2:$N$12,MATCH(A3,$K$2:$K$12,0)),INDEX($O$2:$O$12,MATCH(A3,$K$2:$K$12,0)))+1

To break this down


means keep the left part of the index above. Find the length to keep from the digits keep column


means add a bunch of zeros to the end so you can use a level higher then the current one. Find the digit you need to start with, and the number of digits you want to use. Add 1 to this value.

The & sign between them will concatenate them together.


Place 1in A2.

In cell A3 and below, place this formula: