Skip to main content

How to combine multiple rows into one based on duplicates in Excel?

Maybe, you have the following screenshot data, in the Product name column A, there are some duplicate items, and now you need to remove the duplicate entries in column A but combine the corresponding values in column B, see screenshots:

doc-advanced-combine1 -2 doc-advanced-combine2

Do you have some effective ways to solve this task?

Combine multiple duplicate rows into one with VBA code

Combine multiple duplicate rows into one Kutools for Excel

Combine multiple duplicate rows and sum/average the values with Kutools for Excel


arrow blue right bubbleCombine multiple duplicate rows into one with VBA code

The following VBA code can help you combine duplicate rows based on the value of column A, and the following code is only applied for two columns. So, if you have two columns data, this code will do a favor for you.

1. Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: combine multiple rows into one

Sub CombineRows()
'Update 20131202
Dim WorkRng As Range
Dim Dic As Variant
Dim arr As Variant
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Set Dic = CreateObject("Scripting.Dictionary")
arr = WorkRng.Value
For i = 1 To UBound(arr, 1)
    xvalue = arr(i, 1)
    If Dic.Exists(xvalue) Then
        Dic(arr(i, 1)) = Dic(arr(i, 1)) & " " & arr(i, 2)
    Else
        Dic(arr(i, 1)) = arr(i, 2)
    End If
Next
Application.ScreenUpdating = False
WorkRng.ClearContents
WorkRng.Range("A1").Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.keys)
WorkRng.Range("B1").Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.items)
Application.ScreenUpdating = True
End Sub

3. Then press F5 key, and a prompt box pops out to remind you to select a range that you need to combine. See screenshot:

doc-advanced-combine3

4. And then click OK, the values in column B have been combined into one record and separated by spaces.

doc-advanced-combine4

Notes:

1. This code can destroy the original data, when you use it, you’d better back up the data first.

2. This code is only applied for two columns, if you have more than two columns, this code will only combine first two columns and remove other columns data. When you use this code, you must be cautious.


arrow blue right bubbleCombine multiple duplicate rows into one Kutools for Excel

Now that the above code has some limitations and risks, is there a handy and safe way to finish this job? With Kutools for Excel’s Advanced Combine Rows utility, you can quickly remove multiple duplicate rows in the columns you based on and merge or do some calculations for other records into appropriate cells.

Kutools for Excel includes more than 300 handy Excel tools. Free to try with no limitation in 30 days. Get it Now.

After installing Kutools for Excel, please do as follows:

1. Select the range data that you want to combine duplicates.

doc-advanced-combine5

2. Then click Kutools > Content Converter > Advanced Combine Rows. See screenshot:

doc-advanced-combine6

3. In the Advanced Combine Rows dialog box, select the column name as the key columns that you want to combine values based on, and then click Key columns. See screenshot:

doc-advanced-combine7

4. Then go on clicking the column that you need to combine their values into one record, and then click Combine operations and choose Only combine with separator, and pick one separator you need to separate the combined values from the right list. See screenshot:

doc-advanced-combine8

5. After finishing the settings, click OK, and your data will be merged based on the key columns and separated by separators.

doc-advanced-combine9

Note: If you needn’t to set the operations for the rest columns, just keep the default state.


arrow blue right bubbleCombine multiple duplicate rows and sum/average the values with Kutools for Excel

With this multifunctional Advanced Combine Rows utility, you can combine duplicates rows and do some calculations for the values as well.

1. Select the range data that you want to combine duplicates.

2. Go to the Advanced Combine Rows dialog box;

  • (1.) Select a column name as key column that you want to calculate other column values based on, and then click Key columns;
  • (2.) Select the column that you need to do some calculations for the values, and then click Combine operations > Combine calculate, then choose one calculation type that you need, in this case, I select Sum. See screenshot:

doc-advanced-combine10

3. If you needn’t to set the operations for the rest columns, just keep the default state, then click OK, and the duplicate values in column A will be merged and their corresponding values in column B will be added up. See screenshots:

doc-advanced-combine11 -2 doc-advanced-combine12

Notes:

1. If you check My data has headers option, your headers (the first row) of the range will be ignored when you apply this utility.

2. Use formatted values: it will keep the formatting of the values after combining the duplicate cells.

3. This utility is not available for filtering data.

Click to know more about this Advanced Combine Rows function.


Related article:

Combine duplicate rows and sum the values

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Comments (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I have used the code above successfully in the past however today, it is deleting column B. Looks like it's just removing duplicates...not sure what the issue is.

Sample Data
Reece, Ashley G01-Registered-UWG
Reece, Chris G02-Registered-KSU
Reece, Chris G03-Dropped-KSU
Reece, Jayden G02-Registered-UNG
Reece, Jayden G02-Withdrawn-UNG
This comment was minimized by the moderator on the site
I have in column A

1 Cast
2 Crew
3 Hiring
4 Cast
5 Hiring
6 Crew

I need these unique CAST CREW AND HIRING in another column without delete in column A entries.

Ex: I need (after formula)

1 Cast
2 Crew
3 Hiring

Is it possible by formula?
This comment was minimized by the moderator on the site
Above Macro only Combine the limited cell(e.g 20 cells) in b column. How to Combine more cells
This comment was minimized by the moderator on the site
Same - 2021 and this still only works if you have like 20 rows...
This comment was minimized by the moderator on the site
I've found that the "Combine multiple duplicate rows and sum/average the values" feature within KUTools does not work if the numbers in the row have a comma or period. This makes the tool unusable for combining and summing currency fields (which is why I tried the product in first place). I'm currently using the trial version which states that it is full-functioned; is it possible that this works in the paid version?
This comment was minimized by the moderator on the site
Same here. I was about to purchase because the tools are very nice, but figured I would try it first. Combine never works. I tried it for about 3 weeks now and it always locks up. No matter the power of my computer (ran on my at home powerhouse computer, same issue)
I kept cutting down to less and less data to see if it would work but it still would not.
The last one I tried was 4 columns and 100k rows and it still locked up. Not very useful unless you have like < 100 rows or something.
This comment was minimized by the moderator on the site
Same here, I filtered my data all the way down to 4 columns and 100k rows and it still work not work. I have tried this for 3 weeks now and unable to successfully complete my task. Kutools will only work if you have very few rows.
This comment was minimized by the moderator on the site
[quote]I've found that the "Combine multiple duplicate rows and sum/average the values" feature within KUTools does not work if the numbers in the row have a comma or period. This makes the tool unusable for combining and summing currency fields (which is why I tried the product in first place). I'm currently using the trial version which states that it is full-functioned; is it possible that this works in the paid version?By David Greene[/quote] Sorry, we will fix it in next version.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations