Hello All,
I am working on creating a VBA that takes a data set that has one row with multiple date columns and converts it to a a row for each date column. The Power Query function has worked great for this. The issue I am having is that since the VBA is looking at specific column names, this script will fail next quarter when the timeframe changes. Any ideas how to update the script so that the Power Query works, regardless of the column header names?
Example of workbook column name changes:
[TABLE="width: 960"]
<tbody>[TR]
[TD="class: xl64, width: 256, colspan: 4"]Data this Month[/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[/TR]
[TR]
[TD="class: xl63"]QUANTITY ON ORDER[/TD]
[TD="class: xl63"]TOTAL QUANTITY[/TD]
[TD="class: xl63"]INITIAL[/TD]
[TD="class: xl65"]Jan-19[/TD]
[TD="class: xl65"]Feb-19[/TD]
[TD="class: xl65"]Mar-19[/TD]
[TD="class: xl65"]Apr-19[/TD]
[TD="class: xl65"]May-19[/TD]
[TD="class: xl65"]Jun-19[/TD]
[TD="class: xl65"]Jul-19[/TD]
[TD="class: xl65"]Aug-19[/TD]
[TD="class: xl65"]Sep-19[/TD]
[TD="class: xl65"]Oct-19[/TD]
[TD="class: xl65"]Nov-19[/TD]
[TD="class: xl66"]Dec-19[/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="width: 960"]
<tbody>[TR]
[TD="class: xl66, width: 256, colspan: 4"]Data Next Month[/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[/TR]
[TR]
[TD="class: xl65"]QUANTITY ON ORDER[/TD]
[TD="class: xl65"]TOTAL QUANTITY[/TD]
[TD="class: xl65"]INITIAL[/TD]
[TD="class: xl67"]Mar-19[/TD]
[TD="class: xl67"]Apr-19[/TD]
[TD="class: xl67"]May-19[/TD]
[TD="class: xl67"]Jun-19[/TD]
[TD="class: xl67"]Jul-19[/TD]
[TD="class: xl67"]Aug-19[/TD]
[TD="class: xl67"]Sep-19[/TD]
[TD="class: xl67"]Oct-19[/TD]
[TD="class: xl67"]Nov-19[/TD]
[TD="class: xl67"]Dec-19[/TD]
[TD="class: xl67"]Jan-20[/TD]
[TD="class: xl67"]Feb-20[/TD]
[/TR]
</tbody>[/TABLE]
Kind regards,
Gary
I am working on creating a VBA that takes a data set that has one row with multiple date columns and converts it to a a row for each date column. The Power Query function has worked great for this. The issue I am having is that since the VBA is looking at specific column names, this script will fail next quarter when the timeframe changes. Any ideas how to update the script so that the Power Query works, regardless of the column header names?
Code:
ActiveWorkbook.Queries.Add Name:="Table5", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.CurrentWorkbook(){[Name=""Table5""]}[Content]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Source,{{""VENDOR NUMBER"", type text}, {""VENDOR NAME"", type text}, {""MATERIAL"", type text}, {""DESCRIPTION"", type text}, {""Order Number"", type any}, {""UOM"", type text}, {""Lead Time"", type any}, {""VENDOR PART NUMBER"", type text}, {" & _
"""Branch Plant"", type text}, {""QUANTITY ON ORDER"", Int64.Type}, {""INITIAL"", Int64.Type}, {""Nov-18"", Int64.Type}, {""Dec-18"", Int64.Type}, {""Jan-19"", Int64.Type}, {""Feb-19"", Int64.Type}, {""Mar-19"", Int64.Type}, {""Apr-19"", Int64.Type}, {""May-19"", Int64.Type}, {""Jun-19"", Int64.Type}, {""Jul-19"", Int64.Type}, {""Aug" & _
"-19"", Int64.Type}, {""Sep-19"", Int64.Type}, {""Oct-19"", Int64.Type}})," & Chr(13) & "" & Chr(10) & " #""Unpivoted Other Columns"" = Table.UnpivotOtherColumns(#""Changed Type"", {""VENDOR NUMBER"", ""VENDOR NAME"", ""MATERIAL"", ""DESCRIPTION"", ""Order Number"", ""UOM"", ""Lead Time"", ""VENDOR PART NUMBER"", ""Branch Plant""}, ""Attribute"", ""Value"")," & Chr(13) & "" & Chr(10) & " #""Reordered Columns"" = Tab" & _
"le.ReorderColumns(#""Unpivoted Other Columns"",{""VENDOR NUMBER"", ""VENDOR NAME"", ""MATERIAL"", ""DESCRIPTION"", ""Order Number"", ""Value"", ""UOM"", ""Attribute"", ""Lead Time"", ""VENDOR PART NUMBER"", ""Branch Plant""})," & Chr(13) & "" & Chr(10) & " #""Renamed Columns"" = Table.RenameColumns(#""Reordered Columns"",{{""Value"", ""Quantity""}, {""Attribute"", ""Request Date""}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " " & _
" #""Renamed Columns"""
Example of workbook column name changes:
[TABLE="width: 960"]
<tbody>[TR]
[TD="class: xl64, width: 256, colspan: 4"]Data this Month[/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[/TR]
[TR]
[TD="class: xl63"]QUANTITY ON ORDER[/TD]
[TD="class: xl63"]TOTAL QUANTITY[/TD]
[TD="class: xl63"]INITIAL[/TD]
[TD="class: xl65"]Jan-19[/TD]
[TD="class: xl65"]Feb-19[/TD]
[TD="class: xl65"]Mar-19[/TD]
[TD="class: xl65"]Apr-19[/TD]
[TD="class: xl65"]May-19[/TD]
[TD="class: xl65"]Jun-19[/TD]
[TD="class: xl65"]Jul-19[/TD]
[TD="class: xl65"]Aug-19[/TD]
[TD="class: xl65"]Sep-19[/TD]
[TD="class: xl65"]Oct-19[/TD]
[TD="class: xl65"]Nov-19[/TD]
[TD="class: xl66"]Dec-19[/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="width: 960"]
<tbody>[TR]
[TD="class: xl66, width: 256, colspan: 4"]Data Next Month[/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[/TR]
[TR]
[TD="class: xl65"]QUANTITY ON ORDER[/TD]
[TD="class: xl65"]TOTAL QUANTITY[/TD]
[TD="class: xl65"]INITIAL[/TD]
[TD="class: xl67"]Mar-19[/TD]
[TD="class: xl67"]Apr-19[/TD]
[TD="class: xl67"]May-19[/TD]
[TD="class: xl67"]Jun-19[/TD]
[TD="class: xl67"]Jul-19[/TD]
[TD="class: xl67"]Aug-19[/TD]
[TD="class: xl67"]Sep-19[/TD]
[TD="class: xl67"]Oct-19[/TD]
[TD="class: xl67"]Nov-19[/TD]
[TD="class: xl67"]Dec-19[/TD]
[TD="class: xl67"]Jan-20[/TD]
[TD="class: xl67"]Feb-20[/TD]
[/TR]
</tbody>[/TABLE]
Kind regards,
Gary