How to Merge Duplicates in Excel (6 Ways)

We’ll use the following table to demonstrate how to merge duplicates in Excel.

merge duplicates in excel


Method 1 – Using the IF Function to Merge Duplicates with Text

We will combine the Salesperson’s names for the duplicate products.

merge duplicates in excel

  • We have added two columns, Combined Names and Helper.
  • Select the data range
  • Go to the Home tab, select Sort & Filter and choose Custom Sort.

IF function

  • The Sort dialog box will open.
  • Select the following:

Sort by Product
Sort On Cell Values
Order A to Z

  • Press OK.

IF function

  • This will sort the names of the products from A to Z.

IF function

  • Use the following formula in cell D5.
=IF(B5=B4,D4&", "&C5,C5)
  • B5=B4 → “Apple”= “Product”
  • IF(B5=B4, D4&”, “&C5, C5) IF( “Apple”= “Product”, D4&”, “&C5,C5) becomes
    IF( FALSE, D4&”, “&C5, “Robin”) → as here the logical condition is FALSE so it will return only the Salesperson’s name otherwise it will combine the value of a cell of the Combined Names column and the value of the following cell of the Salesperson column with the help of the Ampersand operator.
    Output → Robin

merge duplicates in excel

  • Hit Enter and drag down the Fill Handle tool.

IF function

  • This combines the names of the Salesperson for the duplicate rows.

IF function

  • Use the following formula in cell E5.
=IF(B6<>B5,"useful","")
  • IF(B6<>B5,”useful”,””) IF(“Apple” <> “Apple”,”useful”,””) returns
    IF(FALSE,”useful”,””) → as the values are equal and so IF will return a Blank
    Output → Blank

IF function

  • Hit Enter and drag down the Fill Handle.

IF function

  • You will get useful for the duplicate rows.

IF function

  • Select the data range.
  • From Sort & Filter, choose Filter.

merge duplicates in excel

  • The filter signs will appear in every column of the table.
  • Select the drop-down of the Helper column.

IF function

  • Choose the useful option and click OK.

IF function

Result:

merge duplicates in excel


Method 2 – Using the Merge & Center Option to Merge Duplicates in Excel

We’ll merge the cells in the Product column that have the same values.

merge duplicates in excel

  • Follow the Steps of Method 1 to sort the product names from A to Z.
  • Select the two cells containing Apple.
  • Go to the Home tab and click on Merge & Center.

Merge & Center option

  • You’ll get a message box. Press OK.

Merge & Center option

  • This merges the first two cells containing Apple.

Merge & Center option

  • Repeat for other duplicate cells to get the following table.

merge duplicates in excel


Method 3 – Using Power Query 

We’ll merge the duplicate rows in the Product column.

merge duplicates in excel

  • Go to the Data tab and select From Table/Range.

power query

  • The Create Table dialog box will open.
  • Select the data range.
  • Check My table has headers and press OK.

power query

  • A Power Query Editor will appear

power query

  • Select the Product column where you have duplicate values.
  • Go to Home and select Group By.

power query

  • The Group By wizard will pop up.
    Select the following options:

Basic
Product (the name of the column)
New column name → Count
Operation → Count Rows

  • Press OK.

power query

Result:

merge duplicates in excel


Method 4 – Using a Pivot Table to Merge Duplicates in Excel

We’ll merge the duplicate rows in Product and sum up their corresponding Sales values.

merge duplicates in excel

  • Go to Insert and select PivotTable.

pivot table

  • The PivotTable from table or range dialog box will open.
  • Select the data range.
  • Check New Worksheet and press OK.

pivot table

  • A new sheet will appear where you have two panels, PivotTable1 on the left side and PivotTable Fields on the right side.

pivot table

  • Drag Product to the Rows area and Sales to the Values area.

pivot table

Result:

pivot table


Method 5 – Using the Consolidate Option to Merge Duplicates in Excel

We’ll merge the duplicate rows and sum up their corresponding Sales values.

merge duplicates in excel

Steps:

  • Select the cell where you want to get the output.
  • Go to Data and select Consolidate.

consolidate option

  • The Consolidate wizard will open.
  • Select Sum (or any other function) as Function, data range as Reference, and click on Add.
  • Check the Top row and Left column options and press OK.

consolidate option

  • You will get the merged values in your selected area.

consolidate option

  • We have used borders for the new consolidated table.

merge duplicates in excel


Method 6 – Using VBA Code to Merge Duplicates in Excel

We’ll merge the duplicate rows and sum up their corresponding Sales values.

merge duplicates in excel

  • Go to the Developer tab and select Visual Basic.

VBA Code

  • The Visual Basic Editor will open up.
  • Go to Insert and select Module.

VBA Code

  • A Module will be created.

VBA Code

  • Insert the following code.
Sub MergeDuplicates()

Dim Rng As Range
Dim d As Variant
Dim y As Variant

On Error Resume Next

Set Rng = Application.Selection
TitleId = "Merge Duplicates in Excel"
Set Rng = Application.InputBox("Range", TitleId, Rng.Address, Type:=8)
Set d = CreateObject("Scripting.Dictionary")
y = Rng.Value

For i = 4 To UBound(y, 1)

d(y(i, 1)) = d(y(i, 1)) + y(i, 2)

Next

Application.ScreenUpdating = False

Rng.ClearContents
Rng.Range("A1").Resize(d.Count, 1) = Application.WorksheetFunction.Transpose(d.keys)
Rng.Range("B1").Resize(d.Count, 1) = Application.WorksheetFunction.Transpose(d.items)

Application.ScreenUpdating = True

End Sub

We have declared Rng as Range and d, y as Variant and used On Error Resume Next to ignore the error and continue or resume the code execution to the next cell.

The FOR loop is used for a range of rows starting from i = 4 (as our data table started from row number 4) and the UBOUND function will determine the size of the array.

VBA Code

  • Press F5
  • The Merge Duplicates in Excel (the name of the custom function) wizard will open.
  • Select the range and press OK.

VBA Code

Result:

merge duplicates in excel


Practice Section

We have provided a Practice section like below in a sheet named Practice so you can test these methods.

practice


Download the Practice Workbook


Merge Duplicates in Excel: Knowledge Hub



<< Go Back to Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Tanjima Hossain
Tanjima Hossain

TANJIMA HOSSAIN is a marine engineer who enjoys working with Excel and VBA programming. For her, programming is a handy, time-saving tool for managing data, files, and online tasks. She's skilled in Rhino3D, Maxsurf C++, MS Office, AutoCAD, and Excel & VBA, going beyond the basics. She holds a B.Sc. in Naval Architecture & Marine Engineering from BUET and is now a content developer. In this role, she creates tech-focused content centred around Excel and VBA. Apart from... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo