Create a unique number list

bristolrob

New Member
Joined
Apr 23, 2018
Messages
34
Ok so here is what I am after if anyone can help
I need to create a unique number from an item list, I am given indent numbers on the list for parent/child relationships
top level is level zero which is 001, next level with an indent of 1 will be 001001, next level with an indent of 2 will be 001001001, there may be a second indent of 2 under that giving you 001001002, and again there could be a 3rd,4th,5th indent of 2 underneath that giving you 001001005, this may go to up to 10 indents, the indents will always go up in sequence before possibly going back down in sequence Or back to level 1 again, so the indents might go 1,2,3,4,5,4,3 then jump back to 1,2,3,2,3,3,2 etc and then back to 1 again, and so on and so on down the item list.
This may be upwards of 20k items for an engine or vehicle etc.
The only way i have of doing this at the moment is manually which is extremely time consuming.
Any advice or help will be gratefully received.
Rob
Example below
A previous formula offered to solve this went up the list in correct sequence, but did not address going back down the list
(MY fault not the fault of the solution offered, many thanks to Peter_SSs)

INDENTUnique number
0001
1001001
2001001001
2001001002
2001001003
1001002
2001002001
2001002002
2001002003
1001003
2001003001
2001003002
2001003003
2001003004
2001003005
2001003006
2001003007
2001003008
1001004
2001004001
2001004002
2001004003
2001004004
2001004005
2001004006
2001004007
3001004007001
3001004007002
4001004007002001
3001004007003
2001004008
1001005
2001005001
3001005001001
2001005002
3001005002001
1001006

<tbody>
</tbody>
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Try:


<b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: rgb(255,255,255);border-collapse: collapse; border-color: rgb(187,187,187)"><thead><tr style=" background-color: rgb(218,231,245);color: rgb(22,17,32)"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: rgb(218,231,245);color: rgb(22,17,32)">C3</th><td style="text-align:left">=INT(<font color="Blue">C2*1000^(<font color="Red">A3-A2</font>)+1</font>)</td></tr></tbody></table></td></tr></table><br />




Excel 2013/2016
ABC
1INDENTUnique numberUnique number Test
2011
3110011001
4210010011001001
5210010021001002
6210010031001003
7110021002
8210020011002001
9210020021002002
10210020031002003
11110031003
12210030011003001
13210030021003002
14210030031003003
15210030041003004
16210030051003005
17210030061003006
18210030071003007
19210030081003008
20110041004
21210040011004001
22210040021004002
23210040031004003
24210040041004004
25210040051004005
26210040061004006
27210040071004007
28310040070011004007001
29310040070021004007002
30410040070020011004007002001
31310040070031004007003
32210040081004008
33110051005
34210050011005001
35310050010011005001001
36210050021005002
37310050020011005002001
38110061006
Sheet1
 
Last edited:
Upvote 0
Many thanks for the prompt reply
Any way to get the 00 at the front of level zero ?
Also when i get down to level 4,s and 5s i get the following results

31005036008
31005036009
41.00504E+12
41.00504E+12
41.00504E+12
41.00504E+12
51.00504E+15
51.00504E+15
51.00504E+15
41.00504E+12
21005037
21005038
31005038001

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


Is this likely to be a formatting issue ?
 
Upvote 0
it is a formatting issue, you need to select the column and format it as NUMBER (no decimals).

for the first value you can type '001 or format the cell as 000 (only the first cell)
 
Upvote 0
Any way to get the 00 at the front of level zero ?
This adaptation of VBA Geek's suggestion would cope with that issue ...

Leave B1 blank.
B2, copied down.

Excel Workbook
AB
1INDENT
20001
31001001
42001001001
52001001002
62001001003
71001002
82001002001
92001002002
102001002003
111001003
122001003001
132001003002
142001003003
152001003004
162001003005
172001003006
182001003007
192001003008
201001004
212001004001
222001004002
232001004003
242001004004
252001004005
262001004006
272001004007
283001004007001
293001004007002
304001004007002001
313001004007003
322001004008
331001005
Unique Num



... but you would still have the same issue with larger number indents due to Excel's limits of significant digits.


I think this formula, copied down, does what you want.

Excel Workbook
AB
1INDENTUnique number
20001
31001001
42001001001
52001001002
62001001003
71001002
82001002001
92001002002
102001002003
111001003
122001003001
132001003002
142001003003
152001003004
162001003005
172001003006
182001003007
192001003008
201001004
212001004001
222001004002
232001004003
242001004004
252001004005
262001004006
272001004007
283001004007001
293001004007002
304001004007002001
313001004007003
322001004008
331001005
342001005001
353001005001001
362001005002
373001005002001
381001006
392001006001
403001006001001
414001006001001001
425001006001001001001
436001006001001001001001
446001006001001001001002
457001006001001001001002001
468001006001001001001002001001
479001006001001001001002001001001
4810001006001001001001002001001001001
499001006001001001001002001001002
509001006001001001001002001001003
518001006001001001001002001002
Unique Num (2)
 
Upvote 0
Good evening Peter
You have cracked it sir !!!
It works all the way down my lists.
This has saved me a serious amount of time.
Any way i can thank you ?
Please let me know
Warmest regards and Bravo !!!
 
Upvote 0
Good evening Peter
You have cracked it sir !!!
It works all the way down my lists.
Good news!
I was wondering though if the sheet becomes a bit sluggish with such a formula copied down 20,000 rows?
If it is, you could also consider this macro that should produce the same results very quickly. If the 'Indent' numbers can change, we might have to do a bit more as, at the moment, the results of the macro will not change if an indent number changes. Anyway, you could give it a try in a copy of your workbook.
Code:
Sub Indent_Numbers()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  Dim s As String
  
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1) As String
  b(1, 1) = "001"
  For i = 2 To UBound(a)
    If a(i, 1) = 1 Then
      k = k + 1
      b(i, 1) = b(1, 1) & Format(k, "000")
    Else
      Select Case Sgn(a(i, 1) - a(i - 1, 1))
        Case -1
          b(i, 1) = Left(s, Len(s) - 6) & Format(Left(Right(s, 6), 3) + 1, "000")
        Case 0
          b(i, 1) = Left(s, Len(s) - 3) & Format(Right(s, 3) + 1, "000")
        Case 1
          b(i, 1) = s & "001"
      End Select
    End If
    s = b(i, 1)
  Next i
  Range("B2").Resize(UBound(b)).Value = b
End Sub



Any way i can thank you ?
Please let me know
Warmest regards and Bravo !!!
You just did. :biggrin:
 
Upvote 0
Good morning Peter

For some reason my formula is no longer returning the results i am after

Could you just confirm the correct cell formatting for column A and column B

Many many thanks as ever

Robert
 
Upvote 0
Could you just confirm the correct cell formatting for column A and column B
Mine are formatted as General


For some reason my formula is no longer returning the results i am after
That doesn't give me much to go on. ;)

- Please identify which formula you are referring to.

- Could we have some small sample data & explain what results the formula is now returning together with what it should be returning?
 
Upvote 0
Using the formula below with both column A and B set to general cell format,

=IF(A2=0,"001",IF(A2=1,B$2&TEXT(COUNTIF(A$2:A2,1),"000"),CHOOSE(SIGN(A2-A1)+2,LEFT(B1,LEN(B1)-6)&TEXT(LEFT(RIGHT(B1,6),3)+1,"000"),LEFT(B1,LEN(B1)-3)&TEXT(RIGHT(B1,3)+1,"000"),B1&"001")))

It is now returning the below results, as you can see it is now for some reason not going back to the required level when going down i.e from 4 to 2 or from 4 to 1 which it did do before (very odd) it is also not returning the leading 2 zero's
I admit i have not used it for a few weeks which is why i asked the question about formatting the cells.


INDENT
01
11001
11002
21002001
31002001001
31002001002
31002001003
31002001004
31002001005
21002002
31002002001
41002002001001
41002002001002
41002002001003
41002002001004
21002002002
21002002003
31002002003001
41002002003001001
41002002003001002
41002002003001003
41002002003001004
41002002003001005
41002002003001006
41002002003001007
41002002003001008
41002002003001009
11002002003002
11002002003003
11002002003004
11002002003005

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


What it should be returning is below

INDENT
0001
1001001
1001002
2001002001
3001002001001
3001002001002
3001002001003
3001002001004
3001002001005
2001002002
3001002002001
4001002002001001
4001002002001002
4001002002001003
4001002002001004
2001002003
2001002004
3001002004001
4001002004001001
4001002004001002
4001002004001003
4001002004001004
4001002004001005
4001002004001006
4001002004001007
4001002004001008
4001002004001009
1001003
1001004
1001005
1001006

<colgroup><col><col></colgroup><tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

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