Consolidate Duplicate Excel Rows by Multiple Columns with VBA
Question details
Consolidate Excel records that match across several identifying columns and combine their numeric data.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A progress log contains multiple rows for the same combination of project, activity sequence, department, and batch or build.
- Observed behavior
- The desired result is one row per matching combination, with the required numeric values summed.
Check that row 1 contains headers, columns A through D identify each record, and column G contains numbers or blanks to sum. Review which other columns should be kept or combined; the example retains their values from the first matching row.
Group matching Excel rows with a VBA dictionary
Use this macro to group rows by columns A through D and sum column G without modifying the source worksheet.
The example reads columns A through I from the active worksheet and writes values to a new worksheet. It retains the first matching row's values in columns E, F, H, and I; change the code if those fields also need combining.
Keys use the underlying cell values and their types, not displayed formatting. The length-prefixed key prevents ambiguous matches such as 1 + 23 and 12 + 3. This example uses Scripting.Dictionary, which may not be available in every Excel environment.
In Excel, select the source worksheet, press Alt+F11, and choose Insert > Module. If you need to retain the macro, save the workbook as an Excel Macro-Enabled Workbook (.xlsm).
Paste this code into the module: Sub ConsolidateProgress() Dim src As Worksheet, dst As Worksheet Dim lastCell As Range, dict As Object Dim lastRow As Long, r As Long, c As Long, outRow As Long Dim key As String, part As String, v As Variant Set src = ActiveSheet Set lastCell = src.Range("A:I").Find(What:="*", After:=src.Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, SearchFormat:=False) If lastCell Is Nothing Then Exit Sub lastRow = lastCell.Row If lastRow < 2 Then MsgBox "No data rows found." Exit Sub End If ' Validate keys and numeric values before creating output. For r = 2 To lastRow If Application.CountA(src.Range("A" & r & ":I" & r)) > 0 Then For c = 1 To 4 If IsError(src.Cells(r, c).Value2) Then MsgBox "Fix the error in row " & r & " before running the macro." Exit Sub End If Next c v = src.Cells(r, 7).Value2 If IsError(v) Then MsgBox "Fix the error in column G, row " & r & "." Exit Sub End If If Len(CStr(v)) > 0 Then If Not IsNumeric(v) Then MsgBox "Column G must contain numbers or blanks. Check row " & r & "." Exit Sub End If End If End If Next r Set dict = CreateObject("Scripting.Dictionary") dict.CompareMode = vbBinaryCompare Set dst = src.Parent.Worksheets.Add(After:=src.Parent.Worksheets(src.Parent.Worksheets.Count)) dst.Range("A1:I1").Value2 = src.Range("A1:I1").Value2 outRow = 1 For r = 2 To lastRow If Application.CountA(src.Range("A" & r & ":I" & r)) > 0 Then key = "" For c = 1 To 4 v = src.Cells(r, c).Value2 part = CStr(VarType(v)) & ":" & CStr(v) key = key & Len(part) & ":" & part Next c If Not dict.Exists(key) Then outRow = outRow + 1 dict.Add key, outRow dst.Range("A" & outRow & ":I" & outRow).Value2 = src.Range("A" & r & ":I" & r).Value2 dst.Cells(outRow, 7).Value2 = 0 End If v = src.Cells(r, 7).Value2 If Len(CStr(v)) > 0 Then dst.Cells(dict(key), 7).Value2 = CDbl(dst.Cells(dict(key), 7).Value2) + CDbl(v) End If End If Next r MsgBox "Consolidated records are on the new worksheet." End Sub
Return to the source worksheet, open Developer > Macros, select ConsolidateProgress, and click Run. Compare several groups and their column G totals on the new worksheet with the source records.

Summarize matching rows with an Excel PivotTable
Use a PivotTable when you need grouped totals but do not need a VBA macro or all original non-key fields.
Group duplicate records with a PivotTable in WPS Spreadsheet
Use a PivotTable in WPS Spreadsheet to group records by multiple fields and sum a numeric column without writing a macro. This produces a summary rather than preserving every non-key field from the first row.
- 1. Open the workbook: Open the XLSX workbook in WPS Spreadsheet and select the data range, including its headers.
- 2. Insert a PivotTable: Choose Insert > PivotTable and place the PivotTable on a new worksheet.
- 3. Add grouping fields: Drag the identifying fields, such as project, activity sequence, department, and batch or build, to the Rows area.
- 4. Sum the numeric field: Drag the field to total to Values and set its summary calculation to Sum if needed. Check the totals against the source data.

Frequently Asked Questions
Why can concatenating A2, B2, C2, and D2 produce incorrect matches?
Without boundaries between values, different combinations can produce the same text. For example, 1 and 23 concatenate to the same result as 12 and 3. The macro uses length-prefixed parts to distinguish them.
Does the VBA macro delete duplicate source rows?
No. It writes consolidated values to a new worksheet and leaves the source worksheet unchanged.
What happens to columns that are not summed?
The example retains columns E, F, H, and I from the first matching row. Modify the macro if those fields need a different rule, such as combining text or choosing the latest date.
Why should I avoid using End(xlDown) to find the last row?
End(xlDown) can stop at a blank cell or jump past the intended data. The macro uses Find across columns A through I to locate the last occupied row.
Can I use Remove Duplicates instead of summing the rows?
Data > Remove Duplicates can retain one matching record, but it does not add the numeric values from the removed rows. Use the macro or a PivotTable when you need totals.




