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
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I supposed your first Date is in A3 (this File) at Sheet1:
Book1(AutoRecovered).xlsm
ABCDEFGHIJ
1Line 1Line 2Line 3
2DateProduct #QuantityDateProduct #QuantityDateProduct #Quantity
39/6/2021451210009/6/202145195009/6/202145121000
49/7/2021589620009/7/202155557009/7/202158962000
59/8/2021247830009/8/202125899009/8/202124783000
69/8/2021357840009/8/2021304511009/8/2021b10
79/8/2021128750009/8/2021168713009/8/202112875000
89/9/2021128760009/9/2021622215009/9/202169996000
9
10
Sheet1


Then with this macro you see result at sheet2:
VBA Code:
Sub SortData()
Dim i As Long, j As Long, Lr As Long, Sh1 As Worksheet, Sh2 As Worksheet
Set Sh1 = Sheets("Sheet1")
Set Sh2 = Sheets("Sheet2")
Lr = Sh1.Range("A" & Rows.Count).End(xlUp).Row
Sh2.Range("A2:D2").Value = Array("Date", "Line", "Product#", "Quantity")
For i = 3 To 3 * Lr - 4
Sh2.Range("A" & i).Value = Sh1.Cells(Int((i - 3) / 3) + 3, ((i - 3) Mod 3) * 3 + 1).Value
Sh2.Range("B" & i).Value = (i - 3) Mod 3 + 1
Sh2.Range("C" & i).Value = Sh1.Cells(Int((i - 3) / 3) + 3, ((i - 3) Mod 3) * 3 + 2).Value
Sh2.Range("D" & i).Value = Sh1.Cells(Int((i - 3) / 3) + 3, ((i - 3) Mod 3) * 3 + 3).Value
Next i
End Sub
 
Upvote 0
I supposed your first Date is in A3 (this File) at Sheet1:
Book1(AutoRecovered).xlsm
ABCDEFGHIJ
1Line 1Line 2Line 3
2DateProduct #QuantityDateProduct #QuantityDateProduct #Quantity
39/6/2021451210009/6/202145195009/6/202145121000
49/7/2021589620009/7/202155557009/7/202158962000
59/8/2021247830009/8/202125899009/8/202124783000
69/8/2021357840009/8/2021304511009/8/2021b10
79/8/2021128750009/8/2021168713009/8/202112875000
89/9/2021128760009/9/2021622215009/9/202169996000
9
10
Sheet1


Then with this macro you see result at sheet2:
VBA Code:
Sub SortData()
Dim i As Long, j As Long, Lr As Long, Sh1 As Worksheet, Sh2 As Worksheet
Set Sh1 = Sheets("Sheet1")
Set Sh2 = Sheets("Sheet2")
Lr = Sh1.Range("A" & Rows.Count).End(xlUp).Row
Sh2.Range("A2:D2").Value = Array("Date", "Line", "Product#", "Quantity")
For i = 3 To 3 * Lr - 4
Sh2.Range("A" & i).Value = Sh1.Cells(Int((i - 3) / 3) + 3, ((i - 3) Mod 3) * 3 + 1).Value
Sh2.Range("B" & i).Value = (i - 3) Mod 3 + 1
Sh2.Range("C" & i).Value = Sh1.Cells(Int((i - 3) / 3) + 3, ((i - 3) Mod 3) * 3 + 2).Value
Sh2.Range("D" & i).Value = Sh1.Cells(Int((i - 3) / 3) + 3, ((i - 3) Mod 3) * 3 + 3).Value
Next i
End Sub
Hi

Looks very much like the solution i am looking for.

If i would like to format the date DDMMYYYY...
How to do this ?

And i also would like to filter out all lines where product # is not a number.
 
Upvote 0
Accomplished with Power Query. Here is the Mcode for the steps taken

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type any}, {"Line 1", type any}, {"Column2", type any}, {"Column3", type any}, {"Line 2", type any}, {"Column4", type any}, {"Column5", type any}, {"Line 3", type any}, {"Column6", type any}}),
    #"Demoted Headers" = Table.DemoteHeaders(#"Changed Type"),
    #"Transposed Table" = Table.Transpose(#"Demoted Headers"),
    #"Merged Columns" = Table.CombineColumns(#"Transposed Table",{"Column1", "Column2"},Combiner.CombineTextByDelimiter(":", QuoteStyle.None),"Merged"),
    #"Transposed Table1" = Table.Transpose(#"Merged Columns"),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table1", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Column1:Date", type date}, {"Column3:Date", type date}, {"Column5:Date", type date}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"Column1:Date", "Column3:Date", "Column5:Date"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Column3:Date", "Column5:Date"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Removed Columns", "Attribute", Splitter.SplitTextByDelimiter(":", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1", type text}, {"Attribute.2", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type2", "Product#", each if Text.StartsWith([Attribute.2],"Product") then [Value] else null),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Quantity", each if Text.StartsWith([Attribute.2],"Quantity") then [Value] else null),
    #"Filled Up" = Table.FillUp(#"Added Custom1",{"Quantity"}),
    #"Replaced Value" = Table.ReplaceValue(#"Filled Up","Line","",Replacer.ReplaceText,{"Attribute.1"}),
    #"Filtered Rows" = Table.SelectRows(#"Replaced Value", each ([Attribute.1] = " 1" or [Attribute.1] = " 2" or [Attribute.1] = " 3")),
    #"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows",{"Attribute.2", "Value"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"Attribute.1", "Line"}, {"Column1:Date", "Date"}}),
    #"Changed Type3" = Table.TransformColumnTypes(#"Renamed Columns",{{"Product#", Int64.Type}}),
    #"Removed Errors" = Table.RemoveRowsWithErrors(#"Changed Type3", {"Product#"})
in
    #"Removed Errors"
 
Upvote 0
And if you want Formula Method:
Book1(AutoRecovered).xlsm
ABCDEFGHIJKLMNO
1Line 1Line 2Line 3DateLineProduct#Quantity
2DateProduct #QuantityDateProduct #QuantityDateProduct #Quantity9/6/2021145121000
39/6/2021451210009/6/202145195009/6/2021451210009/6/202124519500
49/7/2021589620009/7/202155557009/7/2021589620009/6/2021345121000
59/8/2021247830009/8/202125899009/8/2021247830009/7/2021158962000
69/8/2021357840009/8/2021304511009/8/2021b109/7/202125555700
79/8/2021128750009/8/2021168713009/8/2021128750009/7/2021358962000
89/9/2021128760009/9/2021622215009/9/2021699960009/8/2021124783000
99/8/202122589900
109/8/2021324783000
119/8/2021135784000
129/8/2021230451100
139/8/20213b10
149/8/2021112875000
159/8/2021216871300
169/8/2021312875000
179/9/2021112876000
189/9/2021262221500
199/9/2021369996000
20
21
Sheet1
Cell Formulas
RangeFormula
K2:K19K2=INDEX($A$3:$I$8,INT((ROWS($K$1:K1)-1)/3)+1,MOD(ROWS($K$1:K1)-1,3)*3+1)
L2:L19L2=MOD(ROWS($K$1:K2)-2,3) + 1
M2:M19M2=INDEX($A$3:$I$8,INT((ROWS($K$1:K1)-1)/3)+1,MOD(ROWS($K$1:K1)-1,3)*3+COLUMNS($K$1:L1))
N2:N19N2=INDEX($A$3:$I$8,INT((ROWS($K$1:K1)-1)/3)+1,MOD(ROWS($K$1:K1)-1,3)*3+COLUMNS($K$1:M1))
 
Upvote 0
And if you want Formula Method:
Book1(AutoRecovered).xlsm
ABCDEFGHIJKLMNO
1Line 1Line 2Line 3DateLineProduct#Quantity
2DateProduct #QuantityDateProduct #QuantityDateProduct #Quantity9/6/2021145121000
39/6/2021451210009/6/202145195009/6/2021451210009/6/202124519500
49/7/2021589620009/7/202155557009/7/2021589620009/6/2021345121000
59/8/2021247830009/8/202125899009/8/2021247830009/7/2021158962000
69/8/2021357840009/8/2021304511009/8/2021b109/7/202125555700
79/8/2021128750009/8/2021168713009/8/2021128750009/7/2021358962000
89/9/2021128760009/9/2021622215009/9/2021699960009/8/2021124783000
99/8/202122589900
109/8/2021324783000
119/8/2021135784000
129/8/2021230451100
139/8/20213b10
149/8/2021112875000
159/8/2021216871300
169/8/2021312875000
179/9/2021112876000
189/9/2021262221500
199/9/2021369996000
20
21
Sheet1
Cell Formulas
RangeFormula
K2:K19K2=INDEX($A$3:$I$8,INT((ROWS($K$1:K1)-1)/3)+1,MOD(ROWS($K$1:K1)-1,3)*3+1)
L2:L19L2=MOD(ROWS($K$1:K2)-2,3) + 1
M2:M19M2=INDEX($A$3:$I$8,INT((ROWS($K$1:K1)-1)/3)+1,MOD(ROWS($K$1:K1)-1,3)*3+COLUMNS($K$1:L1))
N2:N19N2=INDEX($A$3:$I$8,INT((ROWS($K$1:K1)-1)/3)+1,MOD(ROWS($K$1:K1)-1,3)*3+COLUMNS($K$1:M1))
Unfortunately VBA is the only solution in this case...
 
Upvote 0
And i also would like to filter out all lines where product # is not a number.
You want exclude that product from results ( Delete that lines at result) or include then filter
 
Upvote 0
You want exclude that product from results ( Delete that lines at result) or include then filter
Thank you so much for your effort in this.

As i stated... Unfortunately the only solution to this is to use VBA.
it is not an option to apply a filter or to delete rows afterwards.
 
Upvote 0

Forum statistics

Threads
1,215,487
Messages
6,125,085
Members
449,206
Latest member
ralemanygarcia

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