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

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try this version. As well as adding borders
  • I have fixed a small issue where the Item numbers in column A were extending a little too far down in each section (except the last section)
  • Autofitted the columns
VBA Code:
Sub Get_Results_v2()
  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:=xlPart).Row
    vlr = .Cells(.Rows.Count).End(xlUp).Row
    Do
      lr = .Find(What:="MOVEMENT", After:=.Cells(fr), LookAt:=xlPart).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
        .Offset(, -1).Value = Evaluate("row(1:" & d.Count & ")")
        With .Cells(0, 0).Resize(d.Count + 1, 6)
          .BorderAround xlContinuous
          .Borders(xlInsideVertical).LineStyle = xlContinuous
          .Borders(xlInsideHorizontal).LineStyle = xlContinuous
        End With
        .Cells(-1, 2).BorderAround xlContinuous
      End With
      nr = nr + d.Count + 1
      fr = .Find(What:="MOVEMENT", After:=.Cells(fr), LookAt:=xlPart).Row
      d.RemoveAll
    Loop Until fr < lr
  End With
  wsI.Range("A" & Rows.Count).End(xlUp).CurrentRegion.Copy Destination:=wsR.Range("A" & nr + 2)
  With wsR.Range("A" & Rows.Count).End(xlUp).CurrentRegion
    .BorderAround xlContinuous
    .Borders(xlInsideVertical).LineStyle = xlContinuous
    .Borders(xlInsideHorizontal).LineStyle = xlContinuous
  End With
  wsR.Range("B" & nr + 3).Resize(2).NumberFormat = "#,##0.000"
  wsR.Columns("E:F").NumberFormat = "#,##0.000"
  wsR.UsedRange.Columns.AutoFit
End Sub
 
Upvote 0
Thanks Alan again
not really why work for you and not for me.
I downloaded your file and I entered in PQ editor to see with the Mcode and show me the same error , but before I enter in PQ editor will inform me the Mcode was written in a newer version.
 
Upvote 0
Thanks Peter again.

in the beginning and based on your first answering I don't check all of brands , just I check some BRANDS to see if the code works correctly because I expect should show for the rest of brands when show the others brands without problems , but when I check all of brands based on second answering I've found there is missed brand

CODE(1385) doesn't show at all !

may you check it, please?
 
Upvote 0
CODE(1385) doesn't show at all !
Good catch - I had not noticed that. Try these changes (2 values shown in red towards the end of the code)

Rich (BB code):
Sub Get_Results_v3()
  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:=xlPart).Row
    vlr = .Cells(.Rows.Count).End(xlUp).Row
    Do
      lr = .Find(What:="MOVEMENT", After:=.Cells(fr), LookAt:=xlPart).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
        .Offset(, -1).Value = Evaluate("row(1:" & d.Count & ")")
        With .Cells(0, 0).Resize(d.Count + 1, 6)
          .BorderAround xlContinuous
          .Borders(xlInsideVertical).LineStyle = xlContinuous
          .Borders(xlInsideHorizontal).LineStyle = xlContinuous
        End With
        .Cells(-1, 2).BorderAround xlContinuous
      End With
      nr = nr + d.Count + 2
      fr = .Find(What:="MOVEMENT", After:=.Cells(fr), LookAt:=xlPart).Row
      d.RemoveAll
    Loop Until fr < lr
  End With
  wsI.Range("A" & Rows.Count).End(xlUp).CurrentRegion.Copy Destination:=wsR.Range("A" & nr + 1)
  With wsR.Range("A" & Rows.Count).End(xlUp).CurrentRegion
    .BorderAround xlContinuous
    .Borders(xlInsideVertical).LineStyle = xlContinuous
    .Borders(xlInsideHorizontal).LineStyle = xlContinuous
  End With
  wsR.Range("B" & nr + 3).Resize(2).NumberFormat = "#,##0.000"
  wsR.Columns("E:F").NumberFormat = "#,##0.000"
  wsR.UsedRange.Columns.AutoFit
End Sub
 
Upvote 0
ok.
but will copy to the bottom twice when I run the macro every time .
what's the problem, is relating with your suggestion?
Rich (BB code):
nr = nr + d.Count + 2
 
Upvote 0
For example, in relation to the original sample data?
Show me the problem please.

this is what I got when run macro for every time
split brands.xlsm
ABCDEFG
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
14111288GC 315/80R22.5 AZ124 CHI20735.00014,700.000
15121288GC 315/80R22.5 AZ124 CHI20744.00014,880.000
16131294GC 315/80R22.5 AZ188 CHI20745.00014,900.000
17141385GC 315/80R22.5 AT161 CHI20735.00014,700.000
18MOVEMENT : SELLING
19ITEMCODEBRANDQTYUNIT PRICETOTAL
2011305BS 700R16 R230 JAP2770.0001,540.000
2121306BS 750R16 R230 JAP10775.0007,750.000
2231306BS 750R16 R230 JAP4780.0003,120.000
23
24MOVEMENT : PURCHASING
25DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
262024.01.02114.0001284GC 1200R20 AZ0026 CHI80.000895.000242,560.00
271285GC 1200R20 AZ0183 CHI40.000925.000
281385GC 315/80R22.5 AT161 CHI20.000735.000
291287GC 315/80R22.5 AZ126 CHI20.000735.000
301294GC 315/80R22.5 AZ188 CHI20.000745.000
311241BS 1200R20 G580 JAP40.0002,035.000
321227BS 215/70R15C R623 THI8.000425.000
331221BS 205/70R15C R623 THI12.000405.000
341288GC 315/80R22.5 AZ124 CHI20.000735.000
35DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
362024.01.091211287GC 315/80R22.5 AZ126 CHI14.000735.00010,290.00
371288GC 315/80R22.5 AZ124 CHI20.000744.000
38DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
392024.01.101251227BS 215/70R15C R623 THI2.000425.000850.00
40DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
412024.01.201481287GC 315/80R22.5 AZ126 CHI26.000735.00019,110.00
42DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
432024.01.201491221BS 205/70R15C R623 THI50.000405.000391,650.00
441227BS 215/70R15C R623 THI12.000425.000
451241BS 1200R20 G580 JAP180.0002,035.000
46DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
472024.03.302171285GC 1200R20 AZ0183 CHI10.0001,225.00012,250.00
48DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
492024.04.092261227BS 215/70R15C R623 THI4.000544.0002,176.00
50DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
512024.04.142301285GC 1200R20 AZ0183 CHI40.0001,205.00081,950.00
521284GC 1200R20 AZ0026 CHI30.0001,125.000
53DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
542024.04.172361284GC 1200R20 AZ0026 CHI20.0001,125.00046,600.00
551285GC 1200R20 AZ0183 CHI20.0001,205.000
56MOVEMENT : SELLING
57DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
582024.01.245611306BS 750R16 R230 JAP10.000775.0007,750.00
59DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
602024.01.245621306BS 750R16 R230 JAP2.000780.0001,560.00
61DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
622024.01.245631306BS 750R16 R230 JAP2.000780.0003,100.00
631305BS 700R16 R230 JAP2.000770.000
64SUMMARY
65TOTAL PURCHASE807436
66TOTAL SELLING12410
RESULT
Cell Formulas
RangeFormula
F20:F22,F4:F17F4=D4*E4
 
Upvote 0
I don't believe that result is possible with the code that I provided and original data in the layout you gave as a sample.
In your original sample data for the 'INVOICES' sheet there is a blank row after the main data and before the 'SUMMARY' sections at the bottom.
Could that blank row have disappeared in your 'INVOICES' worksheet?

1725363920918.png
 
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