Skip to main content

Kutools for Office — One Suite. Five Tools. Get More Done.

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

Author Xiaoyang Last modified

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:

a screenshot showing the original data a screenshot of arrow a screenshot showing the combined rows

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


Combine 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:

a screenshot of selecting data range after running the code

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

a screenshot showing the result

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.


Combine 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.

a screenshot of selecting the source data

2. Then click Kutools > Merge & Split > Advanced Combine Rows.

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.

a screenshot of selecting a key column in the Combine Rows Based on Column dialog box

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:

a screenshot of selecting a combine separator in the Combine Rows Based on Column dialog box

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

a screenshot of the combined result

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


Combine 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:

a screenshot showing how to set the Combine Rows Based on Column dialog box

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:

a screenshot of the original data range an arrow a screenshot of the combined result

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
Use Kutools in your preferred language – supports English, Spanish, German, French, Chinese, and 40+ others!

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...


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!

All Kutools add-ins. One installer

Kutools for Office suite bundles add-ins for Excel, Word, Outlook & PowerPoint plus Office Tab Pro, which is ideal for teams working across Office apps.

Excel Word Outlook Tabs PowerPoint
  • All-in-one suite — Excel, Word, Outlook & PowerPoint add-ins + Office Tab Pro
  • One installer, one license — set up in minutes (MSI-ready)
  • Works better together — streamlined productivity across Office apps
  • 30-day full-featured trial — no registration, no credit card
  • Best value — save vs buying individual add-in