How to quickly merge rows based on one column value then do some calculations in Excel?
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.
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.
Tabbed browsing & editing multiple Excel workbooks/Word documents as Firefox, Chrome, Internet Explore 10!
You may be familiar to view multiple webpages in Firefox/Chrome/IE, and switch between them by clicking corresponding tabs easily. Here, Office Tab supports similar processing, which allow you to browse multiple Excel workbooks or Word documents in one Excel window or Word window, and easily switch between them by clicking their tabs. Click for free 30-day trial of Office Tab!
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
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)
.CompareMode = vbTextCompare
For Each Rng In InputRng
If Not .Exists(Rng.Value) Then
.Add Rng.Value, Rng.Offset(, 1)
.Item(Rng.Value).Value = .Item(Rng.Value).Value + Rng.Offset(, 1)
If nRng Is Nothing Then
Set nRng = Rng
Set nRng = Union(nRng, Rng)
If Not nRng Is Nothing Then
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:
3. After selecting a working range, click OK. Now the data has been merged by first column and sum the values in second column.
Note: This VBA code just can correctly work when merging based on the first column and sum values in the second column.
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.
1. Select the data you want to use and click Kutools > Merge & Split > Advanced Combine Rows. See screenshot:
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.
3. Then click Ok. Now the data has been merge based on the primary column, and another is sum up. See screenshot:
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.
Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
More than 300 powerful features. Supports Office/Excel
2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features
30-day free trial. 60-day money back guarantee.
Hey, your module used to work for big tables as well (eg. you want to sum up according to Year but you have Sales 1, Sales 2, Sales 3....etc). But now it only gets the first column right (Sales 1) and all others are not summed correctly. Did you change anything?
Hi, IK, maybe you can use the sumproduct function SUMPRODUCT((A2:A10=”Pen”)*(B2:E10)) to calculate them one by one. A2:A10 is the range contain the lookup value, pen is the criterion, B2:E10 is range that you want to sum based on criterion.