How sort duplicates data under each other of them for each duplicates dates

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
647
Office Version
  1. 2019
Hello ,

I search for macro to deal with about 8000 rows to sort duplicates data based on ref , date columns(E,H).

should sort data by put duplicates ref under each other of them for each date and arrange based on orders for each ref is written first to last for each date.



orders
EFGHIJK
1DATEBRANDINV NOREFENTEROUTNET
201/11/2024BS 1200R20 G580 JAPBSJ2000-1NBFDG MG2,000.00200.001,800.00
301/11/2024BS 1200R20 G580 THIBSJ2000-1VBF MHJY63,000.00100.002,900.00
401/11/2024BS 1200R24 G580 THIBSJ2000-1TR200 MNB62,200.00120.002,080.00
501/11/2024BS 1200R24 G580 JAPBSJ2000-1TT400012,000.00130.0011,870.00
601/11/2024BS 1200R24 G550 JAPBSJ2000-1TT400112,000.00140.0011,860.00
701/11/2024BS 1200R20 G580 JAPBSJ2000-2NBFDG MG1,200.00200.001,000.00
801/11/2024BS 1200R20 G580 THIBSJ2000-2VBF MHJY61,300.00120.001,180.00
901/11/2024BS 1200R20 G580 THIBSJ2000-3VBF MHJY61,700.0080.001,620.00
1001/11/2024BS 1200R24 G580 JAPBSJ2000-3TT40001,800.0090.001,710.00
1101/11/2024BS 1200R20 G580 JAPBSJ2000-3NBFDG MG1,900.0060.001,840.00
1202/11/2024BS 1200R20 G580 THIBSJ2000-4VBF MHJY62,000.0034.001,966.00
1302/11/2024BS 1200R24 G580 JAPBSJ2000-4TT40003,000.0078.002,922.00
1402/11/2024BS 1200R24 G580 THIBSJ2000-4TR200 MNB61,200.00123.001,077.00
1502/11/2024BS 1200R20 G580 JAPBSJ2000-4NBFDG MG4,000.0024.003,976.00
1602/11/2024BS 1200R24 G580 THIBSJ2000-5TR200 MNB62,300.0067.002,233.00
1702/11/2024BS 1200R20 G580 JAPBSJ2000-5NBFDG MG1,200.00987.00213.00
1802/11/2024BS 1200R24 G580 JAPBSJ2000-5TT40001,400.00124.001,276.00
19TOTAL54,200.002,677.0051,523.00
ENTER
Cell Formulas
RangeFormula
I19:J19I19=SUM(I2:I18)
K19K19=I19-J19



should sort like this
orders
EFGHIJK
1DATEBRANDINV NOREFQTYOUTNET
201/11/2024BS 1200R20 G580 JAPBSJ2000-1NBFDG MG2,000.00200.001,800.00
301/11/2024BS 1200R20 G580 JAPBSJ2000-2NBFDG MG1,200.00200.001,000.00
401/11/2024BS 1200R20 G580 JAPBSJ2000-3NBFDG MG1,900.0060.001,840.00
501/11/2024BS 1200R20 G580 THIBSJ2000-1VBF MHJY63,000.00100.002,900.00
601/11/2024BS 1200R20 G580 THIBSJ2000-2VBF MHJY61,300.00120.001,180.00
701/11/2024BS 1200R20 G580 THIBSJ2000-3VBF MHJY61,700.0080.001,620.00
801/11/2024BS 1200R24 G580 THIBSJ2000-1TR200 MNB62,200.00120.002,080.00
901/11/2024BS 1200R24 G580 JAPBSJ2000-1TT400012,000.00130.0011,870.00
1001/11/2024BS 1200R24 G580 JAPBSJ2000-3TT40001,800.0090.001,710.00
1101/11/2024BS 1200R24 G550 JAPBSJ2000-1TT400112,000.00140.0011,860.00
1202/11/2024BS 1200R20 G580 THIBSJ2000-4VBF MHJY62,000.0034.001,966.00
1302/11/2024BS 1200R24 G580 JAPBSJ2000-4TT40003,000.0078.002,922.00
1402/11/2024BS 1200R24 G580 JAPBSJ2000-5TT40001,400.00124.001,276.00
1502/11/2024BS 1200R24 G580 THIBSJ2000-4TR200 MNB61,200.00123.001,077.00
1602/11/2024BS 1200R24 G580 THIBSJ2000-5TR200 MNB62,300.0067.002,233.00
1702/11/2024BS 1200R20 G580 JAPBSJ2000-4NBFDG MG4,000.0024.003,976.00
1802/11/2024BS 1200R20 G580 JAPBSJ2000-5NBFDG MG1,200.00987.00213.00
19TOTAL54,200.002,677.0051,523.00
ENTER
Cell Formulas
RangeFormula
I19:J19I19=SUM(I2:I18)
K19K19=I19-J19


note: orders will be different in sorting for each date based on who ref start writing.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Seems to be almost identical to the question asked today by your colleague here. Try modifying the suggestions there.
 
Upvote 0
Hi ,
I prefer your code because of it's short.:)
well, I try to modify to implement in the same sheet and should also show formula in last column for last row by subtract column I from column J , I don't need the formulas in NET column for each cell.
the code will show compile error in this word .Resize in this line
VBA Code:
Range("I1:N1").Resize (UBound(a))
here is modification .
VBA Code:
Sub Rearrange()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  Dim s As String
 
  Set d = CreateObject("Scripting.Dictionary")
  Application.ScreenUpdating = False
  a = Range("E1", Range("K" & Rows.Count).End(xlUp)).Value
  For i = 2 To UBound(a)
    s = a(i, 1) & "|" & a(i, 4)
    If Not d.exists(s) Then d(s) = d.Count + 1
    a(i, 7) = d(s)
  Next i
    Range("E1:K1").Resize (UBound(a))

    .EntireColumn.ClearContents
    .Value = a
    .Sort Key1:=.Columns(7), Order1:=xlAscending, Header:=xlYes
    '.Columns(6).Offset(1).Resize(UBound(a) - 1).FormulaR1C1 = "=RC[-2]-RC[-1]+N(R[-1]C)*(RC[-5]=R[-1]C[-5])*(RC[-3]=R[-1]C[-3])"
    .Cells(UBound(a), 4).Resize(, 2).FormulaR1C1 = "=SUM(R2C:R[-1]C)"
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
I try to modify to implement in the same sheet
Glad you tried. (y)

Your effort was not too bad and there are usually multiple ways to get a particular result.
The problem with the Error line you had is that it needed a "With" in front of it. However I have removed that whole line in my suggestion below.
Anyway, this is how I would do what I think you are after. Test with a copy.

VBA Code:
Sub Rearrange()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  Dim s As String
 
  Set d = CreateObject("Scripting.Dictionary")
  Application.ScreenUpdating = False
  With Range("E1", Range("K" & Rows.Count).End(xlUp))
    a = .Value
    For i = 2 To UBound(a)
      s = a(i, 1) & "|" & a(i, 4)
      If Not d.exists(s) Then d(s) = d.Count + 1
      a(i, 7) = d(s)
    Next i
    .Value = a
    .Sort Key1:=.Columns(7), Order1:=xlAscending, Header:=xlYes
    .Columns(7).Offset(1).Resize(UBound(a) - 1).FormulaR1C1 = "=RC[-2]-RC[-1]"
    .Columns(7).Resize(UBound(a) - 1).Value = .Columns(7).Resize(UBound(a) - 1).Value
    .Cells(UBound(a), 5).Resize(, 2).FormulaR1C1 = "=SUM(R2C:R[-1]C)"
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
You are welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,224,735
Messages
6,180,635
Members
452,992
Latest member
TokugawaIesuma

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