Skip to main content

How to quickly merge rows based on one column value then do some calculations in Excel?

Author: Sun Last Modified: 2024-10-22

For example, you have a range of data and one column has duplicates, now you want to merge rows bases the column A (has duplicates) then do some calculations to another column based on the merged rows as screenshot shown:

In Excel, there is no quick method to merge rows based on one column value, but here I introduce the tricks that can help you merge duplicate rows then sum or do other calculations on another column.


arrow blue right bubble Merge rows based on one column then sum values with VBA

There is a VBA code can help you to merge the duplicate rows then sum another column values.

1. Press F11 + Alt keys to enable Microsoft Visual Basic for Applications window, then click Insert > Module and copy and paste below VBA code to the new Module window.

VBA: Merge rows based on one column value

Sub MG30Nov12()
'Updateby20150519
Dim Rng As Range
Dim InputRng As Range
Dim nRng As Range
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
Set InputRng = InputRng.Parent.Range(InputRng.Columns(1).Address)
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Rng In InputRng
    If Not .Exists(Rng.Value) Then
        .Add Rng.Value, Rng.Offset(, 1)
    Else
        .Item(Rng.Value).Value = .Item(Rng.Value).Value + Rng.Offset(, 1)
            If nRng Is Nothing Then
                Set nRng = Rng
            Else
                Set nRng = Union(nRng, Rng)
            End If
    End If
Next
If Not nRng Is Nothing Then
    nRng.EntireRow.Delete
End If
End With
End Sub

2. Press F5 or click Run button to run the VBA, and a dialog pops out for selecting a data range to work. see screenshot:

A screenshot showing the data selection prompt for merging rows based on one column in Excel using VBA

3. After selecting a working range, click OK. Now the data has been merged by first column and sum the values in second column.

A screenshot showing merged rows and summed values using VBA in Excel

Note: This VBA code just can correctly work when merging based on the first column and sum values in the second column.


arrow blue right bubble Merge rows based on one column then sum values with Advanced Combine Rows

If you are not familiar with VBA code, you can use the Advanced Combine Rows feature of the third part add-in tool – Kutools for Excel to easily and quickly merge the duplicates rows then sum the values in another column.

Kutools for Excel offers over 300 advanced features to streamline complex tasks, boosting creativity and efficiency. Enhanced with AI capabilities, Kutools automates tasks with precision, making data management effortless. Detailed information of Kutools for Excel...         Free trial...

1. Select the data you want to use and click Kutools > Merge & Split > Advanced Combine Rows. See screenshot:

A screenshot showing the Kutools Advanced Combine Rows option in Excel

2. Then in the popping dialog, select the column which you will merge other columns based on then click Primary Key, and click another column and click Calculate then select Sum.

Tip: If your selected range has headers, check My data has headers, and check Use formatted values will keep the formatting after merging.

A screenshot showing the options in the Kutools Combine Rows dialog for merging and summing

3. Then click Ok. Now the data has been merge based on the primary column, and another is sum up. See screenshot:

A screenshot showing the merged rows and summed values after using Kutools


arrow blue right bubble Merge rows based on one column then do different operations on other columns with Advanced Combine Rows.

Advanced Combine Rows is powerful, because it not only can merge duplicate rows in one column then sum another column, but also can merge duplicate rows in one column then do different operations on other columns.

Suppose I have a range of data as below screenshot shown, and I want to both combine values in Column B and sum values in Column C based on Column A.
A screenshot showing a dataset with duplicate values where merging and different operations are required

Kutools for Excel - Packed with over 300 essential tools for Excel. Enjoy permanently free AI features! Download now!

1. Select data range and click Kutools > Merge & Split > Advanced Combine Rows. See screenshot:

A screenshot showing the Kutools Advanced Combine Rows option in Excel

2. In the Combine Rows Based on Column dialog, do like these:

(1) Click at the column you want to combine based on, and click Primary Key;

A screenshot showing the primary key selection in Kutools for merging rows based on a column in Excel

(2) Click at the column you want to combine data, and click Combine, then select one separator you need from the list;

A screenshot showing the combination options in Kutools for merging rows based on one column

(3) Click at the column you want to sum values and click Calculate, and select Sum.

A screenshot showing the summing options in Kutools for merging rows based on one column

3. Click Ok. Now you can see the result as below:

A screenshot showing the result of merging and performing multiple operations in Excel using Kutools

With Advanced Combine Rows feature, you also can merge rows based on one column then count, average, keep 1st or last data in other columns. 

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


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!