Macro to assign unique ID

Hi,

Attached is the excel which has UID as the column name. When i run the macro an unique id should be assigned to each record.
However, unique id should be same for the client name with same 'Plan Renewal Date'. For example in the attached 'R00001' unique id should be assigned to client name 'CEM corporation', 'R00002' unique id should be assinged to client name 'Stabilit America, Inc.'

Format of the unique id should be 'R00001'

Thank you

Renewal-Wizard-Report.xls
pg1533Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TracyVBA DeveloperCommented:
Try this:
Sub GenerateUID()

    Dim lastRow As Long
    Dim i As Long
    Dim UID As String
    Dim UIDNum As String
    
    lastRow = Range("A" & Rows.Count).End(xlUp).Row
    UID = "R00001"

    'Sort by Client Name and Plan Renewal Date
    Rows("2:" & lastRow).Sort Key1:=Range("A2"), Order1:=xlAscending, Key2:=Range("B2") _
        , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
        False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
        :=xlSortNormal
        
    For i = 2 To lastRow
        If i = 2 Then
            Cells(i, 22).Value = UID
        ElseIf Cells(i, 1).Value = Cells(i - 1, 1).Value And Cells(i, 2).Value = Cells(i - 1, 2).Value Then
            Cells(i, 22).Value = UID
        Else
            UIDNum = Right(UID, 5) + 1
            UID = "R" & WorksheetFunction.Text(UIDNum, "00000")
            Cells(i, 22).Value = UID
        End If
    Next i
    
End Sub

Open in new window

Renewal-Wizard-Report-v1.xls
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.