Merging duplicates items after words contains colon based on different price for the same ID

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
Office Version
  1. 2019
Hello,
I would merge quantity in column E for duplicates BRANDS in column D based on column C for each range contains colon for each name MOVEMENT word is existed after colon .
, but when merge QTY should be based on the same price in unit price in column F . so you will note some the same duplicates IDs contain different price , so don't merge QTY for the same duplicates IDs contain different price .
when merge data should brings column C:F from INVOICES sheet and insert TOTAL column and calculate as the formula is existed .
every time when run the macro should replace new data with old data, also should sort data for each range based on column C from small to big based on numbers . .
last thing I have many ranges contains words and suffix :
I realized error about my topic . should merge same ID based on same price, not different price.
I can't correct my topic, sorry about this mistake!!

split brands.xlsm
ABCDEFG
2MOVEMENT : PURCHASING
3DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
42024.01.021141284GC 1200R20 AZ0026 CHI80895.000242,560.000
51285GC 1200R20 AZ0183 CHI40925.000
61385GC 315/80R22.5 AT161 CHI20735.000
71287GC 315/80R22.5 AZ126 CHI20735.000
81294GC 315/80R22.5 AZ188 CHI20745.000
91241BS 1200R20 G580 JAP402,035.000
101227BS 215/70R15C R623 THI8425.000
111221BS 205/70R15C R623 THI12405.000
12DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
132024.01.091211287GC 315/80R22.5 AZ126 CHI14735.00010,290.000
14DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
152024.01.101251227BS 215/70R15C R623 THI2425.000850.000
16DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
172024.01.201481287GC 315/80R22.5 AZ126 CHI26735.00019,110.000
18DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
192024.01.201491221BS 205/70R15C R623 THI50405.000391,650.000
201227BS 215/70R15C R623 THI12425.000
211241BS 1200R20 G580 JAP1802,035.000
22DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
232024.03.302171285GC 1200R20 AZ0183 CHI101,225.00012,250.000
24DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
252024.04.092261227BS 215/70R15C R623 THI4544.0002,176.000
26DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
272024.04.142301285GC 1200R20 AZ0183 CHI401,205.00081,950.000
281284GC 1200R20 AZ0026 CHI301,125.000
29DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
302024.04.172361284GC 1200R20 AZ0026 CHI201,125.00046,600.000
311285GC 1200R20 AZ0183 CHI201,205.000
32MOVEMENT : SELLING
33DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
342024.01.245611306BS 750R16 R230 JAP10775.0007,750.000
35DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
362024.01.245621306BS 750R16 R230 JAP2780.0001,560.000
37DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
382024.01.245631306BS 750R16 R230 JAP2780.0003,100.000
391305BS 700R16 R230 JAP2770.000
40
41SUMMARY
42TOTAL PURCHASE807,436.000
43TOTAL SELLING12,410.000
INVOICES




result


split brands.xlsm
ABCDEF
2MOVEMENT : PURCHASING
3ITEMCODEBRANDQTYUNIT PRICETOTAL
411221BS 205/70R15C R623 THI62405.00025,110.000
521227BS 215/70R15C R623 THI22425.0009,350.000
631227BS 215/70R15C R623 THI4544.0002,176.000
741241BS 1200R20 G580 JAP2202,035.000447,700.000
851284GC 1200R20 AZ0026 CHI80895.00071,600.000
961284GC 1200R20 AZ0026 CHI501,125.00056,250.000
1071285GC 1200R20 AZ0183 CHI40925.00037,000.000
1181285GC 1200R20 AZ0183 CHI101,225.00012,250.000
1291285GC 1200R20 AZ0183 CHI601,205.00072,300.000
13101385GC 315/80R22.5 AT161 CHI20735.00014,700.000
14111294GC 315/80R22.5 AZ188 CHI20745.00014,900.000
15121287GC 315/80R22.5 AZ126 CHI60735.00044,100.000
16MOVEMENT : SELLING
17ITEMCODEBRANDQTYUNIT PRICETOTAL
1811305BS 700R16 R230 JAP2770.0001,540.000
1921306BS 750R16 R230 JAP10775.0007,750.000
2031306BS 750R16 R230 JAP4780.0003,120.000
21
22SUMMARY
23TOTAL PURCHASE807,436.000
24TOTAL SELLING12,410.000
RESULT
Cell Formulas
RangeFormula
F18:F20,F4:F15F4=D4*E4


I hope to don't make any mistake .
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
With Power query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Filtered Rows" = Table.SelectRows(#"Promoted Headers", each ([DATE] <> "DATE")),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"CODE", "BRAND", "UNIT PRICE"}, {{"Quantity", each List.Sum([QTY]), type nullable number}}),
    #"Filtered Rows1" = Table.SelectRows(#"Grouped Rows", each ([CODE] <> null))
in
    #"Filtered Rows1"
 
Upvote 0
Thanks
this is not what I want . should show for every range alone as I said
I would merge quantity in column E for duplicates BRANDS in column D based on column C for each range contains colon for each name MOVEMENT word is existed after colon .
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Added Custom" = Table.AddColumn(Source, "Custom", each if Text.Contains([Column4],"MOVEMENT") then [Column4] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
    #"Removed Top Rows" = Table.Skip(#"Filled Down",1),
    #"Promoted Headers" = Table.PromoteHeaders(#"Removed Top Rows", [PromoteAllScalars=true]),
    #"Grouped Rows" = Table.Group(#"Promoted Headers", {"CODE", "BRAND", "UNIT PRICE", "MOVEMENT : PURCHASING"}, {{"Quantity", each List.Sum([QTY]), type number}}),
    #"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each ([CODE] <> "CODE")),
    #"Renamed Columns" = Table.RenameColumns(#"Filtered Rows",{{"MOVEMENT : PURCHASING", "MOVEMENT"}}),
    #"Filtered Rows2" = Table.SelectRows(#"Renamed Columns", each ([CODE] <> null)),
    #"Reordered Columns" = Table.ReorderColumns(#"Filtered Rows2",{"CODE", "BRAND", "UNIT PRICE", "Quantity", "MOVEMENT"}),
    #"Filtered Rows1" = Table.SelectRows(#"Reordered Columns", each ([CODE] <> null))
in
    #"Filtered Rows1"
 
Upvote 0
Give this a try with a copy of your workbook.

VBA Code:
Sub Get_Results()
  Dim d As Object
  Dim a As Variant
  Dim wsI As Worksheet, wsR As Worksheet
  Dim fr As Long, lr As Long, vlr As Long, i As Long, nr As Long
  Dim s As String
  
  Set wsI = Sheets("INVOICES")
  Set wsR = Sheets("RESULT")
  wsR.UsedRange.EntireColumn.Delete
  Set d = CreateObject("Scripting.Dictionary")
  nr = 2
  With wsI.Columns("D")
    fr = .Find(What:="MOVEMENT :*", LookAt:=xlWhole).Row
    vlr = .Cells(.Rows.Count).End(xlUp).Row
    Do
      lr = .Find(What:="MOVEMENT :*", After:=.Cells(fr), LookAt:=xlWhole).Row - 1
      If lr < fr Then lr = vlr
      a = wsI.Range("C" & fr & ":F" & lr).Value
      For i = 2 To UBound(a)
        If a(i, 1) <> "CODE" Then
          s = a(i, 1) & ";" & a(i, 2) & ";;" & a(i, 4)
          d(s) = d(s) + a(i, 3)
        End If
      Next i
      wsI.Range("B" & fr).Resize(2, 6).Copy Destination:=wsR.Range("A" & nr)
      wsR.Range("A" & nr + 1).Value = "ITEM"
      With wsR.Range("B" & nr + 2).Resize(d.Count)
        .Value = Application.Transpose(d.Keys)
        .TextToColumns DataType:=xlDelimited, Semicolon:=True, Comma:=False, Space:=False, Other:=False
        .Offset(, 2).Value = Application.Transpose(d.Items)
        .Offset(, 4).FormulaR1C1 = "=RC[-2]*RC[-1]"
        .Resize(, 5).Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlNo
      End With
      nr = nr + d.Count + 1
      fr = .Find(What:="MOVEMENT :*", After:=.Cells(fr), LookAt:=xlWhole).Row
      d.RemoveAll
    Loop Until fr < lr
  End With
  wsI.Range("A" & Rows.Count).End(xlUp).CurrentRegion.Copy Destination:=wsR.Range("A" & nr + 2)
  wsR.Range("B" & nr + 3).Resize(2).NumberFormat = "#,##0.000"
  wsR.Columns("E:F").NumberFormat = "#,##0.000"
  With wsR.Range("A3:A" & wsR.Range("C" & Rows.Count).End(xlUp).Row)
    .SpecialCells(xlBlanks).FormulaR1C1 = "=IFERROR(R[-1]C+1,1)"
    .Value = .Value
  End With
End Sub
 
Last edited:
Upvote 0
Thanks Alan.
may you attach file works for you?
actually I face problem , will shows some errors !
I'm not sure why !
 
Upvote 0
Hi Peter again .
I hope you're fine .:)
I'm not sure why the code just run with MOVEMENT: SELLING and ignore MOVEMENT: PURCHASEING totally!
do you have any idea what's the problem?
 
Upvote 0
do you have any idea what's the problem?
No, I copied your XL2BB 'INVOICES' data to my blank INVOICES sheet ..

abdo meghari.xlsm
ABCDEFG
1
2MOVEMENT : PURCHASING
3DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
42024.01.021141284GC 1200R20 AZ0026 CHI80895242560
51285GC 1200R20 AZ0183 CHI40925
61385GC 315/80R22.5 AT161 CHI20735
71287GC 315/80R22.5 AZ126 CHI20735
81294GC 315/80R22.5 AZ188 CHI20745
91241BS 1200R20 G580 JAP402035
101227BS 215/70R15C R623 THI8425
111221BS 205/70R15C R623 THI12405
12DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
132024.01.091211287GC 315/80R22.5 AZ126 CHI1473510290
14DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
152024.01.101251227BS 215/70R15C R623 THI2425850
16DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
172024.01.201481287GC 315/80R22.5 AZ126 CHI2673519110
18DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
192024.01.201491221BS 205/70R15C R623 THI50405391650
201227BS 215/70R15C R623 THI12425
211241BS 1200R20 G580 JAP1802035
22DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
232024.03.302171285GC 1200R20 AZ0183 CHI10122512250
24DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
252024.04.092261227BS 215/70R15C R623 THI45442176
26DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
272024.04.142301285GC 1200R20 AZ0183 CHI40120581950
281284GC 1200R20 AZ0026 CHI301125
29DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
302024.04.172361284GC 1200R20 AZ0026 CHI20112546600
311285GC 1200R20 AZ0183 CHI201205
32MOVEMENT : SELLING
33DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
342024.01.245611306BS 750R16 R230 JAP107757750
35DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
362024.01.245621306BS 750R16 R230 JAP27801560
37DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
382024.01.245631306BS 750R16 R230 JAP27803100
391305BS 700R16 R230 JAP2770
40
41SUMMARY
42TOTAL PURCHASE807436
43TOTAL SELLING12410
INVOICES


.. and created a 'RESULT' sheet and ran the code. This is the result.
I did manually adjust the column widths afterwards on this sheet but the code could do that too if required.

abdo meghari.xlsm
ABCDEF
1
2MOVEMENT : PURCHASING
3ITEMCODEBRANDQTYUNIT PRICETOTAL
411221BS 205/70R15C R623 THI62405.00025,110.000
521227BS 215/70R15C R623 THI22425.0009,350.000
631227BS 215/70R15C R623 THI4544.0002,176.000
741241BS 1200R20 G580 JAP2202,035.000447,700.000
851284GC 1200R20 AZ0026 CHI80895.00071,600.000
961284GC 1200R20 AZ0026 CHI501,125.00056,250.000
1071285GC 1200R20 AZ0183 CHI40925.00037,000.000
1181285GC 1200R20 AZ0183 CHI101,225.00012,250.000
1291285GC 1200R20 AZ0183 CHI601,205.00072,300.000
13101287GC 315/80R22.5 AZ126 CHI60735.00044,100.000
14111294GC 315/80R22.5 AZ188 CHI20745.00014,900.000
1512MOVEMENT : SELLING
16ITEMCODEBRANDQTYUNIT PRICETOTAL
1711305BS 700R16 R230 JAP2770.0001,540.000
1821306BS 750R16 R230 JAP10775.0007,750.000
1931306BS 750R16 R230 JAP4780.0003,120.000
20
21SUMMARY
22TOTAL PURCHASE807,436.000
23TOTAL SELLING12,410.000
RESULT
Cell Formulas
RangeFormula
F4:F14,F17:F19F4=D4*E4
 
Upvote 0
Something to try. There are three lines in the code similar to this

1725281423307.png


In each of those 3 lines, try removing the 3 yellow characters (space colon and asterisk) and also change xlWhole to xlPart
 
Upvote 0
My apologies Peter !🙏
now I know my bad!:eek:
I changed the range to table when test as @alansidman suggest by PQ that's why the code doesn't work well.
your code works excellently.(y)
just I would fix borders problem. I notice the code will not create borders around the cells even if I add it manually the code will delete it. may you fix this problem ,please?
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

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