Power Query + VBA with Changing Column Headers

Garmerr

New Member
Joined
Sep 13, 2017
Messages
10
Office Version
  1. 365
Platform
  1. Windows
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?

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
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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