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 create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
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 ?
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0

Forum statistics

Threads
1,211,435
Messages
6,101,849
Members
447,758
Latest member
jojo3strada

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