Forum Discussion
ZJacobsmeyer1990
Jul 25, 2025Copper Contributor
Data with Mix of Unique and Duplicate IDs - Create new column that creates Unique IDs for all
I have a data set where I have a mix of unique and duplicate transaction IDs. I am trying to figure out a way to create a MACRO that can complete the following on different data extracts with the sam...
HansVogelaar
Jul 25, 2025MVP
Let's say your IDs are in D2 and down.
A formula solution:
Enter the following formula in E2 or another cell in row 2:
=D2&"-"&COUNTIF(D$2:D2,D2)
Fill down.
A macro solution:
Sub CreateUniqueIDs()
Const col = 4 ' column D
Dim m As Long
Application.ScreenUpdating = False
Cells(1, col + 1).EntireColumn.Insert
m = Cells(Rows.Count, col).End(xlUp).Row
With Range(Cells(2, col + 1), Cells(m, col + 1))
.FormulaR1C1 = "=RC[-1]&""-""&COUNTIF(R2C[-1]:RC[-1],RC[-1])"
.NumberFormat = "@"
.Value = .Value
End With
Application.ScreenUpdating = True
End Sub
m_tarler
Jul 25, 2025Bronze Contributor
alternatively with Excel 365 another option not needing to be a macro: Assuming data is in C4:C16 then
=LET(in, C4:C16,
SCAN(0, SEQUENCE(ROWS(in)), LAMBDA(p,q, INDEX(in, q) & "-" & SUM(--(TAKE(in,q)=INDEX(in,q))))))If you want the functionality of a UDF in a macro then create a new NAME and call it UniqueDash and paste this in the Refers to: section
=LAMBDA(in, SCAN(0, SEQUENCE(ROWS(in)), LAMBDA(p,q, INDEX(in, q) & "-" & SUM(--(TAKE(in,q)=INDEX(in,q))))))then you just need to call UniqueDash with the range like
=UniqueDash(C4:C16)cell D4 used the first formula and E4 used the NAME function