Manually consolidating duplicate rows in Excel is a notoriously tedious and error-prone process. When managing complex institutional reports-such as those tracking standard funding sources-datasets frequently repeat key identifiers, leading to fragmented views. Implementing a dynamic formula to merge these duplicates with a delimiter grants analysts the ability to synthesize clean, nested lists instantly. The primary stipulation is that this approach requires modern Excel engines supporting TEXTJOIN and UNIQUE. For example, this allows for effortlessly combining multiple grant award codes under a single donor entity. Below, we outline the exact formula configuration to streamline your data.
In data management and analysis, encountering duplicate records is an everyday occurrence. Sometimes, simply deleting these duplicates isn't the right solution. Instead, you may need to consolidate your data by combining duplicate rows and merging their associated values into a single cell, separated by a delimiter (like a comma, semicolon, or line break).
For instance, if you have a list of customers and the individual products they purchased, you might want to transform multiple rows for the same customer into a single row listing all their purchases separated by commas. In the past, this required complex VBA macros. Today, modern Excel functions make this task incredibly simple, though we will also cover classic methods for older versions of Excel.
To visualize what we are trying to achieve, consider the following raw dataset:
| Employee (Column A) | Project (Column B) |
|---|---|
| Alice | Project Alpha |
| Bob | Project Beta |
| Alice | Project Gamma |
| Charlie | Project Alpha |
| Bob | Project Delta |
Our goal is to merge the duplicates in the "Employee" column and combine their "Project" values, resulting in this clean table:
| Employee | Combined Projects |
|---|---|
| Alice | Project Alpha, Project Gamma |
| Bob | Project Beta, Project Delta |
| Charlie | Project Alpha |
If you are using Excel 365 or Excel 2021, you have access to dynamic arrays. This is by far the easiest, most robust way to combine duplicate rows without touch-ups or VBA.
First, we need to create a unique list of the items from our duplicate column. We will use the UNIQUE function for this.
Assuming your original data is in cells A2:B6, place this formula in cell D2 to generate a unique list of employees:
=UNIQUE(A2:A6)
This will automatically spill down and display Alice, Bob, and Charlie in column D.
Now, in cell E2 (next to the first unique name), enter the following formula:
=TEXTJOIN(", ", TRUE, FILTER($B$2:$B$6, $A$2:$A$6 = D2))
Drag this formula down for the rest of your unique list. Let's break down how this works:
FILTER($B$2:$B$6, $A$2:$A$6 = D2): This looks at the Employee column (A2:A6) and extracts only the Projects (B2:B6) where the employee matches the value in D2 (e.g., "Alice"). For Alice, this returns an array: {"Project Alpha", "Project Gamma"}.TEXTJOIN(", ", TRUE, ...): This takes the array of filtered projects and merges them. The first argument (", ") is the delimiter. The second argument (TRUE) tells Excel to ignore empty cells.What if Alice worked on "Project Alpha" twice, and you only want to list it once in her combined cell? You can nest the UNIQUE function inside your FILTER function:
=TEXTJOIN(", ", TRUE, UNIQUE(FILTER($B$2:$B$6, $A$2:$A$6 = D2)))
If you are using Excel 2016 or 2019, you won't have access to the FILTER or UNIQUE functions. However, you can achieve the exact same result using Power Query (Get & Transform Data), which is built directly into these versions.
Combined.= Table.Group(#"Changed Type", {"Employee"}, {{"Combined", each _, type table [...]}})
= Table.Group(#"Changed Type", {"Employee"}, {{"Combined", each Text.Combine([Project], ", "), type text}})If you need a solution that works across all versions of Excel and updates at the click of a button, a VBA User-Defined Function (UDF) is an excellent option.
Follow these steps to set up the macro:
ALT + F11 to open the VBA Editor.
Function MergeDuplicates(LookupValue As String, LookupRange As Range, ValueRange As Range, Delimiter As String) As String
Dim i As Long
Dim Result As String
For i = 1 To LookupRange.Rows.Count
If LookupRange.Cells(i, 1).Value = LookupValue Then
If Result = "" Then
Result = ValueRange.Cells(i, 1).Value
Else
Result = Result & Delimiter & ValueRange.Cells(i, 1).Value
End If
End If
Next i
MergeDuplicates = Result
End Function
Once pasted, close the VBA editor. You can now use this custom function just like a regular Excel formula! In your worksheet, extract your unique list of names using the standard "Remove Duplicates" tool, and then enter the following formula in the adjacent column:
=MergeDuplicates(D2, $A$2:$A$6, $B$2:$B$6, ", ")
This function searches $A$2:$A$6 for the value in D2, finds all matches, extracts their corresponding values from $B$2:$B$6, and joins them using your specified delimiter (", ").
The best approach depends entirely on your version of Excel and your personal preference:
By mastering these techniques, you can easily clean up messy, repetitive transactional databases and convert them into tidy, reader-friendly summary reports.
Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.