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
 
it is not an option to apply a filter or to delete rows afterwards
I don't understand.
1. you want rows with product# is text in final result and then Filter
2. You don't want them
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I don't understand.
1. you want rows with product# is text in final result and then Filter
2. You don't want them
Sorry for not beeing clear in my answer.

If the product# is a letter = I don't want it on my final list.
If the product# is a number = i would like it on my final list
 
Upvote 0
Try this with a copy of your data.

VBA Code:
Sub CombineData()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, uba2 As Long
  
  a = Range("F6", Range("N" & Rows.Count).End(xlUp)).Value
  uba2 = UBound(a, 2)
  ReDim b(1 To 3 * UBound(a), 1 To 4)
  For i = 1 To UBound(a)
    For j = 1 To uba2 Step 3
      If IsNumeric(a(i, j + 1)) Then
        k = k + 1
        b(k, 1) = a(i, j): b(k, 2) = (j + 2) / 3: b(k, 3) = a(i, j + 1): b(k, 4) = a(i, j + 2)
      End If
    Next j
  Next i
  With Range("A5:D5").Resize(k)
    .Value = b
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Key2:=.Columns(2), Order2:=xlAscending, Key3:=.Columns(4), Order3:=xlAscending, Header:=xlNo
    .Columns(1).NumberFormat = "ddmmyyyy"
  End With
End Sub

My sample data (F:G) and results (A:D)

Lars1.xlsm
ABCDEFGHIJKLMN
4DateLineProduct #QuantityLine 1Line 2Line 3
506092021145121000DateProduct #QuantityDateProduct #QuantityDateProduct #Quantity
6060920212451950006-09-20214512100006-09-2021451950006-09-202145121000
70609202134512100007-09-20215896200007-09-2021555570007-09-202158962000
80709202115896200008-09-20212478300008-09-2021258990008-09-202124783000
9070920212555570008-09-20213578400008-09-20213045110008-09-2021b10
100709202135896200008-09-20211287500008-09-20211687130008-09-202112875000
110809202112478300009-09-20211287600009-09-20216222150009-09-202169996000
1208092021135784000
1308092021112875000
140809202122589900
1508092021230451100
1608092021216871300
1708092021324783000
1808092021312875000
1909092021112876000
2009092021262221500
2109092021369996000
Sheet1 (2)
 
Upvote 0
Try this with a copy of your data.

VBA Code:
Sub CombineData()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, uba2 As Long
 
  a = Range("F6", Range("N" & Rows.Count).End(xlUp)).Value
  uba2 = UBound(a, 2)
  ReDim b(1 To 3 * UBound(a), 1 To 4)
  For i = 1 To UBound(a)
    For j = 1 To uba2 Step 3
      If IsNumeric(a(i, j + 1)) Then
        k = k + 1
        b(k, 1) = a(i, j): b(k, 2) = (j + 2) / 3: b(k, 3) = a(i, j + 1): b(k, 4) = a(i, j + 2)
      End If
    Next j
  Next i
  With Range("A5:D5").Resize(k)
    .Value = b
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Key2:=.Columns(2), Order2:=xlAscending, Key3:=.Columns(4), Order3:=xlAscending, Header:=xlNo
    .Columns(1).NumberFormat = "ddmmyyyy"
  End With
End Sub

My sample data (F:G) and results (A:D)

Lars1.xlsm
ABCDEFGHIJKLMN
4DateLineProduct #QuantityLine 1Line 2Line 3
506092021145121000DateProduct #QuantityDateProduct #QuantityDateProduct #Quantity
6060920212451950006-09-20214512100006-09-2021451950006-09-202145121000
70609202134512100007-09-20215896200007-09-2021555570007-09-202158962000
80709202115896200008-09-20212478300008-09-2021258990008-09-202124783000
9070920212555570008-09-20213578400008-09-20213045110008-09-2021b10
100709202135896200008-09-20211287500008-09-20211687130008-09-202112875000
110809202112478300009-09-20211287600009-09-20216222150009-09-202169996000
1208092021135784000
1308092021112875000
140809202122589900
1508092021230451100
1608092021216871300
1708092021324783000
1808092021312875000
1909092021112876000
2009092021262221500
2109092021369996000
Sheet1 (2)
Hi Peter

Thank you so much

This works perfectly and all in one solution - thanks.
I am quit new to this VBA, but i am trying to "learning by doing".

I'll hope it's okay with an additional question...
If possible i would like to make my data a bit easier and only have one column with date.


MrExcelData.xlsm
FGHIJKL
1Line 1Line 2Line 3
2DateProduct #QuantityProduct #QuantityProduct #Quantity
306-09-202145121.000451950045121.000
407-09-202158962.00058962.000
508-09-202124783.000555570024783.000
608-09-202135784.0002589900b10
708-09-202112875.000
809-09-202112876.000
910-09-202112875.000
1011-09-202130451.100
1112-09-202116871.300
1213-09-202162221.500
1314-09-2021
1415-09-2021
1516-09-202169996.000
Liste til Produktionsordre (2)
 
Upvote 0
i would like to make my data a bit easier and only have one column with date.
Try the code below.

I was bit confused about the required date format of the results as the following did not agree with the image and mini-sheet of the expected results in post 1. However, you can adjust that in the 'NumberFormat' line near the end of the code.
If i would like to format the date DDMMYYYY

VBA Code:
Sub CombineData_v2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, uba2 As Long
  
  a = Range("F3", Range("F" & Rows.Count).End(xlUp)).Resize(, 7).Value
  uba2 = UBound(a, 2)
  ReDim b(1 To 3 * UBound(a), 1 To 4)
  For i = 1 To UBound(a)
    For j = 2 To uba2 Step 2
      If IsNumeric(a(i, j)) And Len(a(i, j)) > 0 Then
        k = k + 1
        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
    Next j
  Next i
  With Range("A2:D2").Resize(k)
    .Value = b
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Key2:=.Columns(2), Order2:=xlAscending, Key3:=.Columns(4), Order3:=xlAscending, Header:=xlNo
    .Columns(1).NumberFormat = "ddmmyyyy"
  End With
End Sub

Again, my data & results.

Lars1.xlsm
ABCDEFGHIJKL
1DateLineProduct #QuantityLine 1Line 2Line 3
206092021145121000DateProduct #QuantityProduct #QuantityProduct #Quantity
3060920212451950006-09-202145121000451950045121000
40609202134512100007-09-20215896200058962000
50709202115896200008-09-202124783000555570024783000
60709202135896200008-09-2021357840002589900b10
70809202112478300008-09-202112875000
80809202113578400009-09-202112876000
90809202111287500010-09-202112875000
10080920212555570011-09-202130451100
11080920212258990012-09-202116871300
120809202132478300013-09-202162221500
130909202111287600014-09-2021
141009202131287500015-09-2021
151109202123045110016-09-202169996000
1612092021216871300
1713092021262221500
1816092021369996000
Sheet4
 
Upvote 0
Try the code below.

I was bit confused about the required date format of the results as the following did not agree with the image and mini-sheet of the expected results in post 1. However, you can adjust that in the 'NumberFormat' line near the end of the code.


VBA Code:
Sub CombineData_v2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, uba2 As Long
 
  a = Range("F3", Range("F" & Rows.Count).End(xlUp)).Resize(, 7).Value
  uba2 = UBound(a, 2)
  ReDim b(1 To 3 * UBound(a), 1 To 4)
  For i = 1 To UBound(a)
    For j = 2 To uba2 Step 2
      If IsNumeric(a(i, j)) And Len(a(i, j)) > 0 Then
        k = k + 1
        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
    Next j
  Next i
  With Range("A2:D2").Resize(k)
    .Value = b
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Key2:=.Columns(2), Order2:=xlAscending, Key3:=.Columns(4), Order3:=xlAscending, Header:=xlNo
    .Columns(1).NumberFormat = "ddmmyyyy"
  End With
End Sub

Again, my data & results.

Lars1.xlsm
ABCDEFGHIJKL
1DateLineProduct #QuantityLine 1Line 2Line 3
206092021145121000DateProduct #QuantityProduct #QuantityProduct #Quantity
3060920212451950006-09-202145121000451950045121000
40609202134512100007-09-20215896200058962000
50709202115896200008-09-202124783000555570024783000
60709202135896200008-09-2021357840002589900b10
70809202112478300008-09-202112875000
80809202113578400009-09-202112876000
90809202111287500010-09-202112875000
10080920212555570011-09-202130451100
11080920212258990012-09-202116871300
120809202132478300013-09-202162221500
130909202111287600014-09-2021
141009202131287500015-09-2021
151109202123045110016-09-202169996000
1612092021216871300
1713092021262221500
1816092021369996000
Sheet4
PERFECT
Thank you very much. This is perfect.
 
Upvote 0
You're welcome. Glad it worked for you. Thanks for the follow-up. :)
 
Upvote 0
You're welcome. Glad it worked for you. Thanks for the follow-up. :)
i am gratefull for all the help i can get :)


Just one last thing.
If i choose to put my data in another sheet like "Sheet1":

How should this be then:

a = Range("F3", Range("F"......
 
Upvote 0
If i choose to put my data in another sheet like "Sheet1":

How should this be then:

a = Range("F3", Range("F"......
Like this. Make sure that you don't miss the "." in front of Range (twice)

VBA Code:
With Sheets("Sheet1")
  a = .Range("F3", .Range("F" & Rows.Count).End(xlUp)).Resize(, 7).Value
End With
 
Upvote 0
Like this. Make sure that you don't miss the "." in front of Range (twice)

VBA Code:
With Sheets("Sheet1")
  a = .Range("F3", .Range("F" & Rows.Count).End(xlUp)).Resize(, 7).Value
End With
Ok...
This generates an error:
1632220532189.png
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,845
Members
449,051
Latest member
excelquestion515

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