VBA combine and sort several lists in to one list

Lars1

Board Regular
Joined
Feb 3, 2021
Messages
154
Office Version
  1. 365
Platform
  1. Windows
Hi

I have a problem that i hope some one in here could help me with.

I have several lists/coloumns i would like to combine and sort.
For specific reasons, this should be solved by VBA and not Pivot.

If a product number is NOT a number, it should not be on the final list.

1632198442991.png



MrExcelData.xlsx
ABCDEFGHIJKLMNO
1
2AFTERBEFORE
3
4DateLineProduct #QuantityLine 1Line 2Line 3
506-09-2021145121.000DateProduct #QuantityDateProduct #QuantityDateProduct #Quantity
606-09-20212451950006-09-202145121.00006-09-2021451950006-09-202145121.000
706-09-2021345121.00007-09-202158962.00007-09-2021555570007-09-202158962.000
807-09-2021158962.00008-09-202124783.00008-09-2021258990008-09-202124783.000
907-09-20212555570008-09-202135784.00008-09-202130451.10008-09-2021b10
1007-09-2021358962.00008-09-202112875.00008-09-202116871.30008-09-202112875.000
1108-09-2021124783.00009-09-202112876.00009-09-202162221.50009-09-202169996.000
1208-09-2021135784.000
1308-09-2021112875.000
1408-09-202122589900
1508-09-2021230451.100
1608-09-2021216871.300
1708-09-2021324783.000
1808-09-2021312875.000
1909-09-2021169996.000
2009-09-2021262221.500
2109-09-2021369996.000
22
23
Liste til Produktionsordre
 
I am i the process of learning this VBA.
Is it too much to ask if you can put in some comments on what you are doing with your code ?
Hope this helps

VBA Code:
Sub CombineData_v3()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, uba2 As Long
  
  With Sheets("Sheet2")
    'Put all the data values into an array that is 7 columns wide and however many rows down.
    a = .Range("F3", .Range("F" & Rows.Count).End(xlUp)).Resize(, 7).Value
  End With
  'This is how 'wide' the array 'a' is. It will be 7. This saves re-calculating it over and over as we loop later in the code.
  uba2 = UBound(a, 2)
  'make an array plent big enough to hold all the data (number of data rows x 3, and 4 'columns' wide)
  ReDim b(1 To 3 * UBound(a), 1 To 4)
  'Work through each row of the array ..
  For i = 1 To UBound(a)
    '.. and across each row, but in jumps of 2 because each set of data has two columns (+ the date)
    ' This is alo where we use the uba2 (7) calculated earlier
    For j = 2 To uba2 Step 2
      'If column 2 (or 4 or 6 as we jump across) contains a number then
      If IsNumeric(a(i, j)) And Len(a(i, j)) > 0 Then
        'Get ready for a new results row
        k = k + 1
        'Put date in 1st col, Line no. in 2nd col, Prod# in 3rd col and Qty in 4th col
        b(k, 1) = a(i, 1): b(k, 2) = j / 2: b(k, 3) = a(i, j): b(k, 4) = a(i, j + 1)
      End If
    'Jump across 2 cols
    Next j
  'Move down to next row
  Next i
  'Get ready to write the results in Sheet1 (k has counted how many rows of results we found)
  With Sheets("Sheet1").Range("A2:D2").Resize(k)
    'Enter the values from the results array 'b'
    .Value = b
    'Sort results 1st by Date, then by Line No. then by Quantity
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Key2:=.Columns(2), Order2:=xlAscending, Key3:=.Columns(4), Order3:=xlAscending, Header:=xlNo
    'Apply appropriate Date format to the first column
    .Columns(1).NumberFormat = "ddmmyyyy"
  End With
End Sub
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hope this helps

VBA Code:
Sub CombineData_v3()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, uba2 As Long
 
  With Sheets("Sheet2")
    'Put all the data values into an array that is 7 columns wide and however many rows down.
    a = .Range("F3", .Range("F" & Rows.Count).End(xlUp)).Resize(, 7).Value
  End With
  'This is how 'wide' the array 'a' is. It will be 7. This saves re-calculating it over and over as we loop later in the code.
  uba2 = UBound(a, 2)
  'make an array plent big enough to hold all the data (number of data rows x 3, and 4 'columns' wide)
  ReDim b(1 To 3 * UBound(a), 1 To 4)
  'Work through each row of the array ..
  For i = 1 To UBound(a)
    '.. and across each row, but in jumps of 2 because each set of data has two columns (+ the date)
    ' This is alo where we use the uba2 (7) calculated earlier
    For j = 2 To uba2 Step 2
      'If column 2 (or 4 or 6 as we jump across) contains a number then
      If IsNumeric(a(i, j)) And Len(a(i, j)) > 0 Then
        'Get ready for a new results row
        k = k + 1
        'Put date in 1st col, Line no. in 2nd col, Prod# in 3rd col and Qty in 4th col
        b(k, 1) = a(i, 1): b(k, 2) = j / 2: b(k, 3) = a(i, j): b(k, 4) = a(i, j + 1)
      End If
    'Jump across 2 cols
    Next j
  'Move down to next row
  Next i
  'Get ready to write the results in Sheet1 (k has counted how many rows of results we found)
  With Sheets("Sheet1").Range("A2:D2").Resize(k)
    'Enter the values from the results array 'b'
    .Value = b
    'Sort results 1st by Date, then by Line No. then by Quantity
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Key2:=.Columns(2), Order2:=xlAscending, Key3:=.Columns(4), Order3:=xlAscending, Header:=xlNo
    'Apply appropriate Date format to the first column
    .Columns(1).NumberFormat = "ddmmyyyy"
  End With
End Sub

Thank you once again Peter.
This is very helpful for me in my learning process.

THANKS
 
Upvote 0
Thank you once again Peter.
This is very helpful for me in my learning process.

THANKS
Hi Peter

Maybe i already haavve used alle my questions here, but i'll try anyway :)

Ot looks like there could be a small change in my data because an extra column named TEXT is added..
This column should NOT be part of the final list, so how can i "jump over" these extra columns ?

I am using your above code without any changes.



MrExcelData.xlsm
FGHIJKLMNOPQ
1Line 1Line 2Line 3
2DateProduct #TEXTQuantityDateProduct #TEXTQuantityDateProduct #TEXTQuantity
306-09-20214512Text 11.00006-09-20214519Text 250006-09-20214512Text 31.000
407-09-20215896Text 12.00007-09-20215555Text 270007-09-20215896Text 32.000
508-09-20212478Text 13.00008-09-20212589Text 290008-09-20212478Text 33.000
608-09-20213578Text 14.00008-09-20213045Text 21.10008-09-2021bText 310
708-09-20211287Text 15.00008-09-20211687Text 21.30008-09-20211287Text 35.000
809-09-20211287Text 16.00009-09-20216222Text 21.50009-09-20216999Text 36.000
Sheet1



MrExcelData.xlsm
ABCD
1DateLineProduct #Quantity
206092021145121.000
30609202124519500
406092021345121.000
507092021158962.000
607092021358962.000
708092021124783.000
808092021135784.000
908092021112875.000
100809202125555700
110809202122589900
1208092021324783.000
1309092021112876.000
1410092021312875.000
1511092021230451.100
1616092021369996.000
Sheet1
 
Upvote 0
I'm confused.

Originally your raw data had a date column for every Line.
In post 14 you changed to having only one date column. My code was re-written to cope with that layout.
Now your latest mini-sheet shows a date column for every Line again. :confused:

What is actually going on in relation to date columns?
 
Upvote 0
I'm confused.

Originally your raw data had a date column for every Line.
In post 14 you changed to having only one date column. My code was re-written to cope with that layout.
Now your latest mini-sheet shows a date column for every Line again. :confused:

What is actually going on in relation to date columns?
So sorry. My mistake...

Only one column with the date, like your code is working with


Line 1Line 2Line 3
DateProduct #TEXTQuantityProduct #TEXTQuantityProduct #TEXTQuantity
06-09-20214512Text 11.0004519Text 25004512Text 31.000
07-09-20215896Text 12.0005555Text 27005896Text 32.000
08-09-20212478Text 13.0002589Text 29002478Text 33.000
08-09-20213578Text 14.0003045Text 21.100bText 310
08-09-20211287Text 15.0001687Text 21.3001287Text 35.000
09-09-20211287Text 16.0006222Text 21.5006999Text 36.000
 
Upvote 0
So sorry. My mistake...

Only one column with the date, like your code is working with
In that case, try ..

VBA Code:
Sub CombineData_v4()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, uba2 As Long
 
  With Sheets("Sheet2")
    'Put all the data values into an array that is 10 columns wide and however many rows down.
    a = .Range("F3", .Range("F" & Rows.Count).End(xlUp)).Resize(, 10).Value
  End With
  'This is how 'wide' the array 'a' is. It will be 10. This saves re-calculating it over and over as we loop later in the code.
  uba2 = UBound(a, 2)
  'make an array plenty big enough to hold all the results (number of data rows x 3, and 4 'columns' wide)
  ReDim b(1 To 3 * UBound(a), 1 To 4)
  'Work through each row of the array ..
  For i = 1 To UBound(a)
    '.. and across each row, but in jumps of 3 because each set of data has 3 columns (+ the date)
    ' This is alo where we use the uba2 calculated earlier
    For j = 2 To uba2 Step 3
      'If a product column contains a number then
      If IsNumeric(a(i, j)) And Len(a(i, j)) > 0 Then
        'Get ready for a new results row
        k = k + 1
        'Put date in 1st col, Line no. in 2nd col, Prod# in 3rd col and Qty in 4th col
        b(k, 1) = a(i, 1): b(k, 2) = (j + 1) / 3: b(k, 3) = a(i, j): b(k, 4) = a(i, j + 2)
      End If
    'Jump across 2 cols
    Next j
  'Move down to next row
  Next i
  'Get ready to write the results in Sheet1 (k has counted how many rows of results we found)
  With Sheets("Sheet1").Range("A2:D2").Resize(k)
    'Enter the values from the results array 'b'
    .Value = b
    'Sort results 1st by Date, then by Line No. then by Quantity
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Key2:=.Columns(2), Order2:=xlAscending, Key3:=.Columns(4), Order3:=xlAscending, Header:=xlNo
    'Apply appropriate Date format to the first column
    .Columns(1).NumberFormat = "ddmmyyyy"
  End With
End Sub
 
Last edited:
Upvote 0
In that case, try ..

VBA Code:
Sub CombineData_v4()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, uba2 As Long
 
  With Sheets("Sheet2")
    'Put all the data values into an array that is 10 columns wide and however many rows down.
    a = .Range("F3", .Range("F" & Rows.Count).End(xlUp)).Resize(, 10).Value
  End With
  'This is how 'wide' the array 'a' is. It will be 10. This saves re-calculating it over and over as we loop later in the code.
  uba2 = UBound(a, 2)
  'make an array plenty big enough to hold all the data (number of data rows x 3, and 4 'columns' wide)
  ReDim b(1 To 3 * UBound(a), 1 To 4)
  'Work through each row of the array ..
  For i = 1 To UBound(a)
    '.. and across each row, but in jumps of 3 because each set of data has 3 columns (+ the date)
    ' This is alo where we use the uba2 calculated earlier
    For j = 2 To uba2 Step 3
      'If a product column contains a number then
      If IsNumeric(a(i, j)) And Len(a(i, j)) > 0 Then
        'Get ready for a new results row
        k = k + 1
        'Put date in 1st col, Line no. in 2nd col, Prod# in 3rd col and Qty in 4th col
        b(k, 1) = a(i, 1): b(k, 2) = (j + 1) / 3: b(k, 3) = a(i, j): b(k, 4) = a(i, j + 2)
      End If
    'Jump across 2 cols
    Next j
  'Move down to next row
  Next i
  'Get ready to write the results in Sheet1 (k has counted how many rows of results we found)
  With Sheets("Sheet1").Range("A2:D2").Resize(k)
    'Enter the values from the results array 'b'
    .Value = b
    'Sort results 1st by Date, then by Line No. then by Quantity
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Key2:=.Columns(2), Order2:=xlAscending, Key3:=.Columns(4), Order3:=xlAscending, Header:=xlNo
    'Apply appropriate Date format to the first column
    .Columns(1).NumberFormat = "ddmmyyyy"
  End With
End Sub
Once again you have done it. THANKS Peter.

I am trying to see if i can understand what is actually going on, but i think i need some basic VBA training :)

My guess is that "b(k, 1)" points out the Column.
But the rest, i am not sure i can figure out

Put date in 1st col, b(k, 1) = a(i, 1):

Line no. in 2nd col, b(k, 2) = (j + 1) / 3:

Prod# in 3rd col and b(k, 3) = a(i, j):

Qty in 4th col b(k, 4) = a(i, j + 2)
 
Upvote 0
As noted in the comments, b is an array to hold the results (?? rows deep and 4 columns wide)

b(k, 1) refers to row k of the array (this increments by one every time we find a valid Product # in the data) and column 1
b(k, 2) refers to row k, column 2 of the array
etc
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,315
Members
449,081
Latest member
tanurai

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