split duplicate data from sheet to another based on three columns together

Hasson

Active Member
Joined
Apr 8, 2021
Messages
406
Office Version
  1. 2016
Platform
  1. Windows
hello
I need sorting data from the first sheet based on columns B,C,D together . so if any duplicate items in column D depends on also the duplicates dates into columns B,C . when split data should be repeated based on column B,C,D together with sum the values in lastrow for each item . I put the result in second sheet.
note: I will increase data in first sheet at needing ,so should change into second sheet.also when split data should be the same borders and formatting as put it.
RESULT (2) (2).xlsm
ABCDE
1ITEMFROM DATETO DATEIDQTY
211/1/20222/11/2022QQW-1 200.00
321/1/20222/11/2022QQW-1 300.00
431/1/20222/11/2022QQW-1 120.00
541/4/20222/14/2022QQW-2100.00
651/5/20222/15/2022QQW-3120.00
761/6/20222/16/2022QQW-490.00
871/7/20222/17/2022QQW-1 80.00
981/8/20222/18/2022QQW-360.00
1091/6/20222/16/2022QQW-450.00
11101/10/20222/20/2022QQW-540.00
12111/11/20222/21/2022QQW-630.00
13121/12/20222/22/2022QQW-7120.00
14131/13/20222/23/2022QQW-8100.00
15141/14/20222/24/2022QQW-9110.00
16151/15/20222/25/2022QQW-1030.00
17161/16/20222/26/2022QQW-1120.00
18171/17/20222/27/2022QQW-12120.00
RP



result
RESULT (2) (2).xlsm
ABCDE
1ID
2QQW-1
3ITEMFROM DATETO DATEQTY
411/1/20222/11/2022200.00
521/1/20222/11/2022300.00
631/1/20222/11/2022120.00
7TOTAL620.00
8
9ID
10QQW-1
11ITEMFROM DATETO DATEQTY
1211/7/20222/17/202280.00
13TOTAL80.00
14
15ID
16QQW-2
17ITEMFROM DATETO DATEQTY
1811/4/20222/14/2022100.00
19TOTAL100.00
20
21ID
22QQW-3
23ITEMFROM DATETO DATEQTY
2411/5/20222/15/2022120.00
25TOTAL120.00
26
27ID
28QQW-4
29ITEMFROM DATETO DATEQTY
3011/6/20222/16/202290.00
3121/6/20222/16/202250.00
32TOTAL140.00
33
34ID
35QQW-5
36ITEMFROM DATETO DATEQTY
3711/10/20222/20/202240.00
38TOTAL40.00
39
40ID
41QQW-6
42ITEMFROM DATETO DATEQTY
4311/11/20222/21/202230.00
44TOTAL30.00
45
46ID
47QQW-7
48ITEMFROM DATETO DATEQTY
4911/12/20222/22/2022120.00
50TOTAL120.00
51
52ID
53QQW-8
54ITEMFROM DATETO DATEQTY
5511/13/20222/23/2022100.00
56TOTAL100.00
57
58ID
59QQW-9
60ITEMFROM DATETO DATEQTY
6111/14/20222/24/2022110.00
62TOTAL110.00
63
64ID
65QQW-10
66ITEMFROM DATETO DATEQTY
6711/15/20222/25/202230.00
68TOTAL30.00
69
70
71ID
72QQW-11
73ITEMFROM DATETO DATEQTYQTY
7411/16/20222/26/202220.0020.00
75TOTAL20.0020.00
76
77ID
78QQW-12
79ITEMFROM DATETO DATEQTY
8011/17/20222/27/2022120.00
81TOTAL120.00
82
83
84
outcome

thanks
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Before running the macro, create a sheet called "Format" and put the following format:
Dante Amor
ABCD
1ID
2
3ITEMFROM DATETO DATEQTY
4
5TOTAL
Format



Try this:
VBA Code:
Sub SplitData()
  Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
  Dim lr As Long, i As Long, j As Long, n As Long, tot As Double
  Dim ant As String
  
  Application.ScreenUpdating = False
  Set sh1 = Sheets("RP")
  Set sh2 = Sheets("Outcome")
  Set sh3 = Sheets("Format")
  
  sh2.Cells.Clear
  lr = sh1.Range("B" & Rows.Count).End(3).Row
  sh1.Range("A1:E" & lr).Sort sh1.[B2], xlAscending, sh1.[C2], , xlAscending, sh1.[D2], xlAscending, xlYes

  ant = sh1.Range("B2").Value & "|" & sh1.Range("D2").Value
  sh3.Range("A1:D5").Copy sh2.Range("A1")
  sh2.Range("C2").Value = sh1.Range("D2").Value
  j = 4
  n = 1
  For i = 2 To lr + 1
    If ant <> sh1.Range("B" & i).Value & "|" & sh1.Range("D" & i).Value Then
      sh2.Range("D" & j).Value = tot
      If i = lr + 1 Then Exit For
      j = j + 2
      n = 1
      sh3.Range("A1:D5").Copy sh2.Range("A" & j)
      sh2.Range("C" & j + 1).Value = sh1.Range("D" & i).Value
      tot = 0
      j = j + 3
    ElseIf n > 1 Then
      sh2.Range("A" & j - 1).EntireRow.Copy
      sh2.Range("A" & j).Insert Shift:=xlDown
    End If
    sh2.Range("A" & j).Value = n
    sh2.Range("B" & j).Value = sh1.Range("B" & i).Value
    sh2.Range("C" & j).Value = sh1.Range("C" & i).Value
    sh2.Range("D" & j).Value = sh1.Range("E" & i).Value
    tot = tot + sh1.Range("E" & i).Value
    n = n + 1
    j = j + 1
    ant = sh1.Range("B" & i).Value & "|" & sh1.Range("D" & i).Value
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Before this line:
Application.ScreenUpdating = True

Add this line:
sh2.Range("A:D").EntireColumn.AutoFit
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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