TransformColumnTypes without knowing column name

trose6382

New Member
Joined
Sep 12, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello,
I'm trying to find a way around knowing the column name of a pdf file I'm trying to upload to Excel. Right now, the code works as long as I know what the first column name is. Unfortunately, the pdf files I upload data from changes the first column name. Is there a way for me to upload data from a pdf and transform the column types without knowing the name of the column being imported? The column name in question is in red and called UnknownColumnName. Thank you in advance!

Rich (BB code):
Sub Upload_PDF_Data()
   
    ' Upload_PDF_Data Macro
   
    Dim PDFtableName As String, tableId As String
    Dim PDFfile As Variant
    Dim loadToCell As Range
    Dim queryName As String
   
    'The name of the table in the selected PDF which will be imported into Excel
   
    PDFtableName = "Page001"
   
    'The cell where the import will begin - the destination cell of the query table created by this macro
   
    Set loadToCell = ActiveSheet.Range("I113")
       
    PDFfile = Application.GetOpenFilename(FileFilter:="PDF Files (*.pdf), *.pdf", MultiSelect:=False, Title:="Select PDF to upload")
    If PDFfile = False Then Exit Sub 'Cancel clicked
   
    queryName = GetQueryName(CStr(PDFfile))
    tableId = PDFtableName
   
 
    ActiveWorkbook.Queries.Add Name:=queryName, Formula:= _
        "let" & Chr(13) & "" & Chr(10) & _
        "    Source = Pdf.Tables(File.Contents(""" & PDFfile & """), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & _
        "    Page1 = Source{[Id=""" & tableId & """]}[Data]," & Chr(13) & "" & Chr(10) & _
        "    #""Promoted Headers"" = Table.PromoteHeaders(Page1, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & _
        "    #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{??UnknownColumnName??, type text}, {""Column2"", type number}, {""Column3"", Int64.Type}, {""Column4"", Int64.Type}, {""Column5"", type number}})" & Chr(13) & "" & Chr(10) & _
        "in" & Chr(13) & "" & Chr(10) & _
        "    #""Changed Type"""
       
       
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""" & queryName & """;Extended Properties=""""", _
            Destination:=loadToCell).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [" & queryName & "]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = queryName
        .Refresh BackgroundQuery:=False
    End With
   
End Sub
 
Last edited by a moderator:

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
You could use
Power Query:
Table.ColumnNames(#""Promoted Headers""){0}
to get the first column name
 
Upvote 0
Solution

Forum statistics

Threads
1,223,876
Messages
6,175,129
Members
452,614
Latest member
MRSWIN2709

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