silversong
New Member
- Joined
- Jan 28, 2016
- Messages
- 2
Hi!
This is my first question on this forum so I will try to be clear and specific as possible. I know it can get frustrating when you guys have to give solutions of unclear questions.
- My purpose
· I have a database of patients, and a userform allows me to manage this database, including adding new entries.
· Each patient needs to have a unique ID (which I call clinic number). Obviously the ID can not be duplicated.
· The ID follows this format C000000ID.
· Upon opening the userform, the ID will appear on Textbox1 prefilled.
- What I managed to achieve
· I managed to use a code to generate sequential ID. e.g. C000001ID, C000002ID, C000003ID etc..
·
· Everything worked great until I delete a specific record. Deleting a record can create a duplicate entry.
· e.g. After deleting C000002ID, the next generated ID will be C000003ID...which already exists.
- My sad sad temporary solution
·
· Basically an If statement that check for a duplicate. In the event of a duplicate, the ID follows the format "C100000ID" instead of "C000000ID"
- What I require.
· All I require is to generate unique ID numbers following the format C000000ID. The "C" and "ID" needs to remain the same. the "000000" need to obviously change. They DO NOT have to be sequntial. I dont mind if they are random numbers...just no duplicate.
haha Phew I tried to make everything seem logical and consistent. Would be really really grateful if someone could help me with this as I am still very new to VB Excel.
Many thanks,
Kasun.
This is my first question on this forum so I will try to be clear and specific as possible. I know it can get frustrating when you guys have to give solutions of unclear questions.
- My purpose
· I have a database of patients, and a userform allows me to manage this database, including adding new entries.
· Each patient needs to have a unique ID (which I call clinic number). Obviously the ID can not be duplicated.
· The ID follows this format C000000ID.
· Upon opening the userform, the ID will appear on Textbox1 prefilled.
- What I managed to achieve
· I managed to use a code to generate sequential ID. e.g. C000001ID, C000002ID, C000003ID etc..
·
Code:
With TextBox1 .Value = "C" & Format(Cells(Rows.Count, 1).End(xlUp).Row + 1, "000000") & "ID"
.Enabled = False
End With
· e.g. After deleting C000002ID, the next generated ID will be C000003ID...which already exists.
- My sad sad temporary solution
·
Code:
Private Sub UserForm_Initialize()Dim ws As Worksheet, iRow As Long
Set ws = Worksheets("Data")
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
'Clinic ID Validation-------------------------
If WorksheetFunction.CountIf(ws.Range("A:A", ws.Cells(iRow, 1)), Me.TextBox1.Value) > 0 Then
With TextBox1
.Value = "C1" & Format(Cells(Rows.Count, 1).End(xlUp).Row + 1, "00000") & "ID"
.Enabled = False
End With
Else
With TextBox1
.Value = "C" & Format(Cells(Rows.Count, 1).End(xlUp).Row + 1, "000000") & "ID"
.Enabled = False
End With
End If
- What I require.
· All I require is to generate unique ID numbers following the format C000000ID. The "C" and "ID" needs to remain the same. the "000000" need to obviously change. They DO NOT have to be sequntial. I dont mind if they are random numbers...just no duplicate.
haha Phew I tried to make everything seem logical and consistent. Would be really really grateful if someone could help me with this as I am still very new to VB Excel.
Many thanks,
Kasun.