VBA to merge duplicate rows and sum values in column

sotplugy

New Member
Joined
Aug 16, 2016
Messages
3
RE: https://www.mrexcel.com/forum/excel...duplicate-rows-sum-values-certain-column.html

Tried the code here but doesn't seem to work well for my case.


Example:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Room[/TD]
[TD]Quantity[/TD]
[TD]Description[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Small -- 2101[/TD]
[/TR]
[TR]
[TD]Small[/TD]
[TD]1[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]Small[/TD]
[TD]1[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]Small[/TD]
[TD]2[/TD]
[TD]Pear[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Small -- 2102[/TD]
[/TR]
[TR]
[TD]Small[/TD]
[TD]1[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]Small[/TD]
[TD]1[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]Small[/TD]
[TD]2[/TD]
[TD]Pear[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Medium -- 3101[/TD]
[/TR]
[TR]
[TD]Medium[/TD]
[TD]1[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]Medium[/TD]
[TD]1[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]Medium[/TD]
[TD]2[/TD]
[TD]Pear[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Medium -- 3102[/TD]
[/TR]
[TR]
[TD]Medium[/TD]
[TD]1[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]Medium[/TD]
[TD]1[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]Medium[/TD]
[TD]2[/TD]
[TD]Pear[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Small -- 2104[/TD]
[/TR]
[TR]
[TD]Small[/TD]
[TD]1[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]Small[/TD]
[TD]2[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]Small[/TD]
[TD]2[/TD]
[TD]Pear[/TD]
[/TR]
</tbody>[/TABLE]


Output:

[TABLE="width: 500"]
<tbody>[TR]
[TD]Room[/TD]
[TD]Quantity[/TD]
[TD]Description[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Small -- 2101, 2102, 2104[/TD]
[/TR]
[TR]
[TD]Small[/TD]
[TD]3[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]Small[/TD]
[TD]3[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]Small[/TD]
[TD]6[/TD]
[TD]Pear[/TD]
[/TR]
[TR]
[TD]Count[/TD]
[TD]3[/TD]
[TD]Total (Small)[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Medium -- 3101, 3102[/TD]
[/TR]
[TR]
[TD]Medium[/TD]
[TD]2[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]Medium[/TD]
[TD]2[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]Medium[/TD]
[TD]4[/TD]
[TD]Pear[/TD]
[/TR]
[TR]
[TD]Count[/TD]
[TD]2[/TD]
[TD]Total (Medium)[/TD]
[/TR]
</tbody>[/TABLE]

Currently have this:


Dim Rng As range, Dn As range, n As Long, nRng As range
Set Rng = range(range("C2"), range("E" & Rows.Count).End(xlUp))
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Dn In Rng
If Not .Exists(Dn.Value) Then
.Add Dn.Value, Dn
Else
If nRng Is Nothing Then Set nRng = _
Dn Else Set nRng = Union(nRng, Dn)
.Item(Dn.Value).Offset(, -1) = .Item(Dn.Value).Offset(, -1) + Dn.Offset(, -1)
End If
Next
If Not nRng Is Nothing Then nRng.EntireRow.Delete
End With


Any ideas how to allow for a sandwiched group (medium) in the groups of Small?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top