Add dynamic column reference in VBA

yits05

Board Regular
Joined
Jul 17, 2020
Messages
56
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I recorded a macro which selects each pdf filepath in column A, transforms the data in powerquery, and then imports the table in a new tab. The macro worked well on the file with which I recorded the Macro. However, each PDF has a different number of columns I need to adjust as part of the powerquery transformation, and I was wondering how I can account for this in my code. Here is the snippet that is tripping the script up:

VBA Code:
ActiveWorkbook.Queries.Add Name:="Table001 (Page 1)", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Pdf.Tables(File.Contents(""""" & filepath & """), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & "    #""Removed Other Columns"" = Table.SelectColumns(Source,{""Id"", ""Data""})," & Chr(13) & "" & Chr(10) & "    #""Expanded Data"" = Table.ExpandTableColumn(#""Removed Other Columns"", ""Data"", {""Column1"", """ & _
        "Column2"", ""Column3"", ""Column4""}, {""Column1"", ""Column2"", ""Column3"", ""Column4""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Expanded Data"""

The error I get is "[Expression.Error] The name 'Source' wasnt recognized. Make sure it's spelled correctly". I suspect this is due to the fact that the VBA calls out specific column names/numbers (column 1, column 2, etc.) which reflects the original file, but other PDFs may not have those same columns. The one consistent rule I need the code to follow is that it should delete all columns not named "Id" or "Data", without the need to reference other columns by name.

Thanks!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Power Query:
#"Columns removed"= Table.SelectColumns(#"Expanded Data",{"Id", "Data"})
 
Upvote 0
Solution

Forum statistics

Threads
1,214,927
Messages
6,122,311
Members
449,080
Latest member
jmsotelo

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