Merge 1000 items and 10000 rows for duplicates items from multiple columns

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
635
Office Version
  1. 2019
Hi experts
I need merging column B,C,D into one column and sum value in column E for duplicates items .
the original data
SP.xlsm
ABCDE
3ITEMBRANDTYPEORIGINBALANCE
41245/40R18RE050AJAP36
52245/40R20RE050AJAP19
63245/45R17RE050AJAP15
74245/45R17EA01JAP1
85245/45R19S-03JAP7
96245/40R20RE050AJAP20
107245/45R17RE050AJAP22
118245/65R17D697INDO25
129245/70R16D694JAP3
1310245/70R16D697INDO1
1411245/75R16D697INDO12
1512245/75R17693AJAP623
1613255/30R19RE050AJAP12
1714255/35R18GR90JAP14
1815255/35R20RE050AJAP8
BMS


the result

SP.xlsm
ABC
3ITEMBRANDBALANCE
41245/40R18 RE050A JAP36
52245/40R20 RE050A JAP39
63245/45R17 RE050A JAP37
74245/45R17 EA01 JAP1
85245/45R19 S-03 JAP7
96245/65R17 D697 INDO25
107245/70R16 D694 JAP3
118245/70R16 D697 INDO1
129245/75R16 D697 INDO12
1310245/75R17 693A JAP623
1411255/30R19 RE050A JAP12
1512255/35R18 GR90 JAP14
1613255/35R20 RE050A JAP8
RESULT

and should update in result sheet based on updating BMS sheet when run the macro every time .
I have about 1000 items and could repeat for 10000 rows
thanks
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You can try a pivot table.

First concatenate the data:
Dante Amor
ABCDEF
1ITEMBRANDTYPEORIGINBALANCEBRAND TYPE ORIGIN
21245/40R18RE050AJAP36245/40R18 RE050A JAP
32245/40R20RE050AJAP19245/40R20 RE050A JAP
43245/45R17RE050AJAP15245/45R17 RE050A JAP
54245/45R17EA01JAP1245/45R17 EA01 JAP
65245/45R19S-03JAP7245/45R19 S-03 JAP
76245/40R20RE050AJAP20245/40R20 RE050A JAP
87245/45R17RE050AJAP22245/45R17 RE050A JAP
98245/65R17D697INDO25245/65R17 D697 INDO
109245/70R16D694JAP3245/70R16 D694 JAP
1110245/70R16D697INDO1245/70R16 D697 INDO
1211245/75R16D697INDO12245/75R16 D697 INDO
1312245/75R17693AJAP623245/75R17 693A JAP
1413255/30R19RE050AJAP12255/30R19 RE050A JAP
1514255/35R18GR90JAP14255/35R18 GR90 JAP
1615255/35R20RE050AJAP8255/35R20 RE050A JAP
Hoja1
Cell Formulas
RangeFormula
F2:F16F2=B2&" "&C2&" "&D2

And then create a pivot table:
1685141727005.png


When you add more information just update the pivot table. :cool:
 
Upvote 0
Hi Abdo, please try the following on a copy of your data. (Assumes your data starts on row 4 as you've indicated).

VBA Code:
Sub Abdo_V1()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("BMS")
    Set ws2 = Worksheets("RESULT")
    
    Dim rng As Range, R As Range, txt As String
    Dim i As Long, j As Long, n As Long, ar
    Set rng = ws1.Range("B4:B" & ws1.Cells(Rows.Count, "B").End(xlUp).Row)
    ar = ws1.Range("B4", ws1.Cells(Rows.Count, "E").End(xlUp))

    With CreateObject("scripting.dictionary")
        For Each R In rng
            txt = R.Value & " " & R.Offset(0, 1).Value & " " & R.Offset(0, 2).Value
        If Not .exists(txt) Then
            n = n + 1
            .Add txt, n
            For j = 1 To UBound(ar, 2)
                ar(n, j) = R.Offset(, j - 1)
            Next j
        Else
            ar(.Item(txt), 4) = ar(.Item(txt), 4) + R.Offset(, 3)
        End If
        Next R
    End With
    
    For i = 1 To UBound(ar)
        ar(i, 2) = ar(i, 1) & " " & ar(i, 2) & " " & ar(i, 3)
        ar(i, 1) = i
        ar(i, 3) = ar(i, 4)
    Next i
    ws2.Range("A3").Resize(, 3).Value = Array("ITEM", "BRAND", "BALANCE")
    ws2.Range("A4").Resize(n, 3) = ar
End Sub
 
Upvote 1
An alternative with Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ITEM", Int64.Type}, {"BRAND", type text}, {"TYPE", type text}, {"ORIGIN", type text}, {"BALANCE", Int64.Type}}),
    #"Merged Columns" = Table.CombineColumns(#"Changed Type",{"BRAND", "TYPE", "ORIGIN"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Merged"),
    #"Grouped Rows" = Table.Group(#"Merged Columns", {"Merged"}, {{"New Balance", each List.Sum([BALANCE]), type nullable number}})
in
    #"Grouped Rows"
Book2
ABCDEFGH
1ITEMBRANDTYPEORIGINBALANCEMergedNew Balance
21245/40R18RE050AJAP36245/40R18 RE050A JAP36
32245/40R20RE050AJAP19245/40R20 RE050A JAP39
43245/45R17RE050AJAP15245/45R17 RE050A JAP37
54245/45R17EA01JAP1245/45R17 EA01 JAP1
65245/45R19S-03JAP7245/45R19 S-03 JAP7
76245/40R20RE050AJAP20245/65R17 D697 INDO25
87245/45R17RE050AJAP22245/70R16 D694 JAP3
98245/65R17D697INDO25245/70R16 D697 INDO1
109245/70R16D694JAP3245/75R16 D697 INDO12
1110245/70R16D697INDO1245/75R17 693A JAP623
1211245/75R16D697INDO12255/30R19 RE050A JAP12
1312245/75R17693AJAP623255/35R18 GR90 JAP14
1413255/30R19RE050AJAP12255/35R20 RE050A JAP8
1514255/35R18GR90JAP14
1615255/35R20RE050AJAP8
Sheet1
 
Upvote 0
I don't used PT .
if it's possible by Vba.

Then try this macro:


VBA Code:
Sub MergeItems()
  Dim dic As Object, sh As Worksheet, a, i&, ky$
  Set sh = Sheets("BMS")
  Set dic = CreateObject("Scripting.Dictionary")
  
  a = sh.Range("A4", sh.Range("E" & Rows.Count).End(3)).Value
  For i = 1 To UBound(a, 1)
    ky = Trim(a(i, 2)) & " " & Trim(a(i, 3)) & " " & Trim(a(i, 4))
    dic(ky) = dic(ky) + a(i, 5)
  Next
  With Sheets("RESULT")
    .Cells.ClearContents
    .Range("B4").Resize(dic.Count, 2) = Application.Transpose(Array(dic.keys, dic.items))
    .Range("A4").Value = 1
    .Range("A4").AutoFill .Range("A4:A" & dic.Count + 3), xlFillSeries
    sh.Range("A3, B3, E3").Copy .Range("A3")
  End With
End Sub
 
Upvote 0
can sort from small to big after merging
Hi abdo:

The try this:

VBA Code:
Sub MergeItems()
  Dim dic As Object, sh As Worksheet, a, i&, ky$
  Set sh = Sheets("BMS")
  Set dic = CreateObject("Scripting.Dictionary")
 
  a = sh.Range("A4", sh.Range("E" & Rows.Count).End(3)).Value
  For i = 1 To UBound(a, 1)
    ky = Trim(a(i, 2)) & " " & Trim(a(i, 3)) & " " & Trim(a(i, 4))
    dic(ky) = dic(ky) + a(i, 5)
  Next
  With Sheets("RESULT")
    .Cells.ClearContents
    .Range("B4").Resize(dic.Count, 2) = Application.Transpose(Array(dic.keys, dic.items))
    .Range("B4:C" & dic.Count + 3).Sort key1:=.Range("C3"), order1:=xlAscending, Header:=xlNo
    .Range("A4").Value = 1
    .Range("A4").AutoFill .Range("A4:A" & dic.Count + 3), xlFillSeries
    sh.Range("A3, B3, E3").Copy .Range("A3")
  End With
End Sub
 
Upvote 1
Solution
Hi Dante
first thank you for the code
second I have question about sorting ,does sort this data are correct ?
SP.xlsm
ABC
3ITEMBRANDBALANCE
41245/45R17 EA01 JAP1
52245/70R16 D697 INDO1
63255/50R19 ER30 JAP1
74255/70R15C D697 THI1
8526.5R25 VSDTZ JAP1
96265/65R17 D697 INDO1
107285/45R19 ER30 JAP1
118295/80R22.5 M840 JAP1
129300-15 JL INDO1
1310315/80R22.5 R294 JAP1
1411600-14 LT MRA JAP1
1512825-15 14PR JLA INDO1
16139.5R17.5 M716 JAP1
1714LT 235/85R16 D697 INDO1
1815LT255/70R16 D697 THI1
19161200R20 M857 JAP1
20171200R20 G550 JAP1
2118275/35R20 S001 JAP2
2219275/70R16 D694 JAP2
232029.5R29 **VKT JAP2
2421750R16 FSM JAP2
2522P225/75R16 D697 INDO2
2623245/70R16 D694 JAP3
272429.5R25 **VKT JAP3
2825315/35R20 DHPZ JAP3
29267.5-15/6.00 SOLID PJLS JAP3
3027700-12 JAP3
3128LT215/75R15 D694 JAP3
3229275/40R20 DHPS JAP4
333029.5R29 VSNTZ JAP4
3431750R16 LT D694 JAP4
3532245/45R19 S-03 JAP7
3633255/45ZR17 RE050A JAP7
3734700R20 R187 JAP7
3835255/35R20 RE050A JAP8
3936P265/50R20 DHPA JAP8
4037LT225/75R16 T697 INDO9
4138255/40R17 RE050A JAP10
4239275/35R18 EA01 JAP10
434028×9-15 PL01 JAP10
44416.50-10 10PR BKT PL01 INDIA10
454210-17.5 R180 JAP11
4643245/75R16 D697 INDO12
4744255/30R19 RE050A JAP12
4845275/40R18 RE050A JAP12
494629.5R25 **VSDL JAP12
50471100R20 MICHILEN THI12
5148255/35R18 GR90 JAP14
5249255/40R18 RE001 JAP15
5350600-9 6PR JLA INDO16
5451P235/55R17 D400 JAP16
5552275/35R18 RE050A JAP19
5653285/30R20 RE050A JAP20
5754245/40R22 RE050A JAP20
5855275/55R20 D680 JAP22
5956255/45ZR17 S-01 JAP23
6057245/65R17 D697 INDO25
6158245/40R24 RE050A JAP25
6259275/30R20 RE050A JAP26
6360245/45R17 RE050A JAP37
6461245/40R20 RE050A JAP39
6562700-12 PL01 JAP45
6663285/60R18 H/L850 JAP62
67641200R22.5 R187 JAP79
6865245/75R17 693A JAP623
6966750R16 VSJ JAP1177
RESULT
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

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