Create new ID number without duplicates

eviehc123

New Member
Joined
Jan 21, 2019
Messages
32
Hi all,

I am trying to write a vba to generate unique ID numbers. This number needs to display in cell "I15" and cannot have already been generated.

The unique ID will either need to start with "NHS" or "MAN" depending on which the user is requiring. For example, the ID would look something like this: "NHS001", "NHS002" etc. or "MAN001", "MAN002" etc.

I have attempted many different ways of doing this and am not really getting close!

Can anyone help or point me in the right direction to do this please?

Thank you,
Eve
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Special-K99

Well-known Member
Joined
Nov 7, 2006
Messages
8,350
I wont be answering this but what happens if someone deletes MAN002 ? Do you want another MAN002 or should the next one be the last "MAN" number that was generated plus 1 ?
 

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,388
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
You will need a place to store the IDs already generated to check against them.
Or you can generate a number = 1+ last used number. Still the last used must be stored somewhere after it has been issued.
 

eviehc123

New Member
Joined
Jan 21, 2019
Messages
32
I wont be answering this but what happens if someone deletes MAN002 ? Do you want another MAN002 or should the next one be the last "MAN" number that was generated plus 1 ?

Hi I don't want to use previously used numbers even if they are deleted.
 

eviehc123

New Member
Joined
Jan 21, 2019
Messages
32

ADVERTISEMENT

Can anyone help with adding a prefix to the list below? I want to add "NHS".

Code:
Sub GenerateList()
Range("A1").Value = "Numbers Available"
Range("B1").Value = "Randomize"
Range("c1").Value = "Used Numbers"
Range("A2:A1000").Formula = "=ROW()+1000"
Range("A2:A1000").Value = Range("A2:A1000").Value
Range("B2:B1000").Formula = "=RAND()"
Range("A:B").Sort key1:=Range("B1"), Order1:=xlAscending, Header:=xlYes
Range("B:B").EntireColumn.Delete
Range("A1001:A900000").Clear
End Sub

Thanks
 

Gerald Higgins

Well-known Member
Joined
Mar 26, 2007
Messages
9,115
I'm not a code expert but try this, it seems to work . . . there may be better ways of doing it . . .

Code:
Sub GenerateList()
Range("A1").Value = "Numbers Available"
Range("B1").Value = "Randomize"
Range("c1").Value = "Used Numbers"
Range("A2:A1000").Formula = "=""NHS""&ROW()+1000"
Range("A2:A1000").Value = Range("A2:A1000").Value
Range("B2:B1000").Formula = "=RAND()"
Range("A:B").Sort key1:=Range("B1"), Order1:=xlAscending, Header:=xlYes
Range("B:B").EntireColumn.Delete
Range("A1001:A900000").Clear
End Sub
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
I am trying to write a vba to generate unique ID numbers. The unique ID will either need to start with "NHS" or "MAN" depending on which the user is requiring. For example, the ID would look something like this: "NHS001", "NHS002" etc. or "MAN001", "MAN002" etc.
Can anyone help or point me in the right direction to do this please?

1. Do you want a random unique number (as suggested in post#5) or the next number in sequence?
2. Are there only 2 prefix possibilites NHS & MAN?
3. Are numbers always 3 digit with leading zeros (if less than 100)?
4. Are all historic numbers 3 digits or are some 1 or 2 digit?
5. What happens when all numbers 1 to 999 have been taken?
- when is that likely to occur?
 

Watch MrExcel Video

Forum statistics

Threads
1,129,361
Messages
5,635,812
Members
416,884
Latest member
leeshjay

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
Top