vba to consolidate and sum duplicate row items

solidENM

Board Regular
Joined
Feb 23, 2017
Messages
87
Hello,
I have a parts list, and it shows cut lengths and pieces needed to build a unit. I need some help condensing all duplicates into one line items with total length needed.
the formula would be:
Column B x Column E, and then sum and condense all unique items in column D into one line each, qty 1 with total length of all like units.

I do have other items on these parts lists, screws, fittings, etc. They are all blank in column E, only the planks have a length entered in column E. Column H is the next blank column if that matters at all.


example layout below.

col. a col. b col. c col. d col. e
ITEM NO.QTY.DescriptionPart NumberLength[mm]
11Oak040241346
22Oak040241346
32Mahogany055991346
41Oak 040241267
51Mahogany05599305




<colgroup><col span="2"><col><col><col></colgroup><tbody>
</tbody>



the desired output would be:
(col. a) (col. b) (col. c) (col. d) (col. e)
ITEM NO.QTY.DescriptionPart NumberLength[mm]
11Oak 040245305
21Mahogany055992997

<colgroup><col span="2"><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Assuming your header "ITEM NO." is in A1, this will place the consolidated data beginning in G1 on the same sheet.
Code:
Sub Consolidate()
Dim R As Range, c As Range
Set R = Range("A1:E" & Cells(Rows.Count, "D").End(xlUp).Row)
Application.ScreenUpdating = False
Columns("G:K").ClearContents
R.Columns(4).AdvancedFilter Action:=xlFilterCopy, copytorange:=Range("J1"), unique:=True
Range("G1").Resize(1, 5).Value = Range("A1:E1").Value
For Each c In Range("G2:G" & Cells(Rows.Count, "J").End(xlUp).Row)
    c.Value = c.Row - 1
Next c
Range("H2:H" & Cells(Rows.Count, "J").End(xlUp).Row).Value = 1
For Each c In Range("I2:I" & Cells(Rows.Count, "J").End(xlUp).Row)
    c.Value = Evaluate("INDEX(" & R.Columns(3).Address & ",MATCH(" & c.Offset(0, _
        1).Address & "," & R.Columns(4).Address & ",0))")
Next c
For Each c In Range("K2:K" & Cells(Rows.Count, "J").End(xlUp).Row)
    c.Value = Evaluate("SUMPRODUCT(--(" & R.Columns(4).Address & "=" & _
        c.Offset(0, -1).Address & ")," & R.Columns(2).Address & "," & _
        R.Columns(5).Address & ")")
    c.Value = c.Value
Next c
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Hi JoeMo,
Thanks for the reply. I had just left on vacation when you originally posted, and today is my first day back. I have columns A-I filled out, so I shifted your code to start in Column J. I am having some other issues with this though. I noticed you have a line to make value = 1, and I would need this to be a true total.

Im thinking it would be best if the macro could create a new Sheet, paste this copied info to there, get correct qty from first sheet for matching items, and then delete the original sheet. I have played around with merging the offset columns into the main columns, but its been troublesome. values with a qty >0 in Column E would need to be Qty1 in col B. Everything else would have to be the matching value off the original sheet.

Thanks a lot for helping out, you have brought me to a great starting point. If you or anyone can help me further, that would speed things along. I am more comfortable learning from viewing code, as I can manipulate that further if needed. Its new tricks I need tips and pointers on.



Sub Consolidate()
'
' Consolidate Macro
'


Dim R As Range, c As Range
Set R = Range("A1:E" & Cells(Rows.Count, "D").End(xlUp).Row)
Application.ScreenUpdating = False
Columns("J:N").ClearContents
R.Columns(4).AdvancedFilter Action:=xlFilterCopy, copytorange:=Range("M1"), unique:=True
Range("J1").RESIZE(1, 5).Value = Range("A1:E1").Value
For Each c In Range("J2:J" & Cells(Rows.Count, "M").End(xlUp).Row)
c.Value = c.Row - 1
Next c
Range("K2:K" & Cells(Rows.Count, "M").End(xlUp).Row).Value = 1
For Each c In Range("L2:L" & Cells(Rows.Count, "M").End(xlUp).Row)
c.Value = Evaluate("INDEX(" & R.Columns(3).Address & ",MATCH(" & c.Offset(0, _
1).Address & "," & R.Columns(4).Address & ",0))")
Next c
For Each c In Range("N2:N" & Cells(Rows.Count, "M").End(xlUp).Row)
c.Value = Evaluate("SUMPRODUCT(--(" & R.Columns(4).Address & "=" & _
c.Offset(0, -1).Address & ")," & R.Columns(2).Address & "," & _
R.Columns(5).Address & ")")
c.Value = c.Value
Next c
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,377
Messages
6,124,598
Members
449,174
Latest member
chandan4057

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