Add dynamic column reference in VBA

yits05

Board Regular
Joined
Jul 17, 2020
Messages
52
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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,091
Office Version
  1. 2016
Platform
  1. Windows
Power Query:
#"Columns removed"= Table.SelectColumns(#"Expanded Data",{"Id", "Data"})
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,130,119
Messages
5,640,226
Members
417,130
Latest member
Seanr19871

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
Top