Split 1 column into many but consolidate by date

Jumparound

New Member
Joined
Aug 4, 2015
Messages
45
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I hope I can explain this properly. We have a long list of data in sheet1 (columns B to I). Short excerpt here, the real data is around 3,000 rows.
CustomerBatchSizeQuantityMaterialLocationDateType
Apples
4281​
21​
CF1AR05
09/01/2023​
CF1-3
Apples
5559​
4​
CF1AR05
09/01/2023​
CF1-3
Apples
8050​
4​
CF1AR05
09/01/2023​
CF1-3
Apples
2704​
3​
CF1AR05
09/01/2023​
CF1-3
Apples
7801​
6​
CF1AR05
09/01/2023​
CF1-3
Apples
6361​
3​
CF1AR05
09/01/2023​
CF1-3
Apples
6486​
8​
CF1AR05
09/01/2023​
CF1-3
Apples
6028​
4​
CF4AR05
09/01/2023​
CF 4-6
Apples
7591​
2​
CF6AR05
09/01/2023​
CF 4-6
Apples
5048​
1​
CF4AR05
09/01/2023​
CF 4-6
Apples
2624​
60L
6​
CF1AR05
09/01/2023​
Crates
Apples
6441​
115L
9​
CF1AR05
09/01/2023​
Crates
Apples
2893​
115L
1​
CF1AR05
09/01/2023​
Crates
Apples
5891​
31​
CF1AR02
11/01/2023​
CF1-3
Apples
6376​
11​
CF1AR02
11/01/2023​
CF1-3
Apples
8579​
1​
CF1AR02
11/01/2023​
CF1-3
Apples
9769​
2​
CF1AR02
11/01/2023​
CF1-3
Apples
6429​
3​
CF4AR02
11/01/2023​
CF 4-6
Apples
1159​
1​
CF4AR02
11/01/2023​
CF 4-6
Apples
4117​
3​
CF4AR02
11/01/2023​
CF 4-6
Apples
9729​
4​
CF4AR02
11/01/2023​
CF 4-6
Apples
3458​
2​
CF6AR02
11/01/2023​
CF 4-6
Apples
6001​
4​
CF6AR02
11/01/2023​
CF 4-6
Apples
8116​
60L
2​
CF1AR02
11/01/2023​
Crates
Apples
5045​
115L
5​
CF1AR02
11/01/2023​
Crates
Apples
5750​
IBC
1​
CF1WTSF
11/01/2023​
Crates
Apples
8048​
115L
1​
CF1AR02
11/01/2023​
Crates
Apples
8194​
115L
1​
CF1AR02
11/01/2023​
Crates

I want to separate this into columns in Sheet2 by type (column H) and date but with the minimum number of rows so there is not a lot of unused space as per the example below.

DateCustomerBatchSizeQuantityMaterialLocationTypeCustomerBatchSizeQuantityMaterialLocationTypeCustomerBatchSizeQuantityMaterialLocationType
09/01/2023​
Apples
4281​
21​
CF1AR05CF1-3Apples
6028​
4​
CF4AR05CF 4-6Apples
2624​
60L
6​
CF1AR05Crates
09/01/2023​
Apples
5559​
4​
CF1AR05CF1-3Apples
7591​
2​
CF6AR05CF 4-6Apples
6441​
115L
9​
CF1AR05Crates
09/01/2023​
Apples
8050​
4​
CF1AR05CF1-3Apples
5048​
1​
CF4AR05CF 4-6Apples
2893​
115L
1​
CF1AR05Crates
09/01/2023​
Apples
2704​
3​
CF1AR05CF1-3
09/01/2023​
Apples
7801​
6​
CF1AR05CF1-3
09/01/2023​
Apples
6361​
3​
CF1AR05CF1-3
09/01/2023​
Apples
6486​
8​
CF1AR05CF1-3
11/01/2023​
Apples
5891​
31​
CF1AR02CF1-3Apples
6429​
3​
CF4AR02CF 4-6Apples
8116​
60L
2​
CF1AR02Crates
11/01/2023​
Apples
6376​
11​
CF1AR02CF1-3Apples
1159​
1​
CF4AR02CF 4-6Apples
5045​
115L
5​
CF1AR02Crates
11/01/2023​
Apples
8579​
1​
CF1AR02CF1-3Apples
4117​
3​
CF4AR02CF 4-6Apples
5750​
IBC
1​
CF1WTSFCrates
11/01/2023​
Apples
9769​
2​
CF1AR02CF1-3Apples
9729​
4​
CF4AR02CF 4-6Apples
8048​
115L
1​
CF1AR02Crates
11/01/2023​
Apples
3458​
2​
CF6AR02CF 4-6Apples
8194​
115L
1​
CF1AR02Crates
11/01/2023​
Apples
6001​
4​
CF6AR02CF 4-6

So there are only 7 rows of dates for the 9th Jan because that is the maximum number of rows needed for the most types in that day (7 CF1-3). Then for the 11th Jan there are 6 rows because the maximum that day is type CF 4-6. Is this possible? Thanks!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
We have a long list of data in sheet1 (columns B to I)
Considering that your data starts in cell B1, then in sheet2 the results will also start in cell B1.

Try this macro:
VBA Code:
Sub Split_Data()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim a As Variant, b As Variant, d_ant As Variant, t_ant As Variant
  Dim i&, j&, k&, new_r&, new_c&, kmax&, col&
  
  Set sh1 = Sheets("Sheet1")
  Set sh2 = Sheets("Sheet2")
  
  a = sh1.Range("B1", sh1.Range("I" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To 100)
  
  For i = 2 To UBound(a, 1)
    If d_ant <> a(i, 7) Then
      new_r = kmax + 1
      new_c = 2
      k = new_r
      kmax = k
    Else
      If t_ant <> a(i, 8) Then
        new_c = new_c + 7
        k = new_r
      Else
        k = k + 1
      End If
    End If
    If k > kmax Then kmax = k
    
    b(k, 1) = a(i, 7)
    b(k, new_c) = a(i, 1)
    b(k, new_c + 1) = a(i, 2)
    b(k, new_c + 2) = a(i, 3)
    b(k, new_c + 3) = a(i, 4)
    b(k, new_c + 4) = a(i, 5)
    b(k, new_c + 5) = a(i, 6)
    b(k, new_c + 6) = a(i, 8)
    
    d_ant = a(i, 7)   'date
    t_ant = a(i, 8)   'type
  Next
  
  Application.ScreenUpdating = False
  sh2.Cells.ClearContents
  sh2.Range("B1").Value = "Date"
  sh2.Range("C1:H1").Value = sh1.Range("B1:G1").Value
  sh2.Range("I1").Value = sh1.Range("I1").Value
  sh2.Range("B2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  col = sh2.Cells.Find("*", , xlValues, xlPart, xlByColumns, xlPrevious).Column
  sh2.Range("C1:I1").Copy sh2.Range("J1", sh2.Cells(1, col))
  Application.ScreenUpdating = True
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
 
Upvote 0
Solution

Forum statistics

Threads
1,215,131
Messages
6,123,223
Members
449,091
Latest member
jeremy_bp001

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