Forum Discussion

InThought's avatar
InThought
Copper Contributor
Dec 04, 2024

Extract Same Table from Multiple PDFs

I tried to modify the recorded macro to accept another PDF in table 002 with the next monthly summary table (table001 & table002 confusion). All of the files are in the same folder numbered incrementally, 1-12 (Jan-Dec). There are other similar folders for different accounts requiring the same procedure be performed and are set up in similar increments by account. Would like all of the monthly summaries from the PDF table 001 listed within one worksheet or workbook.

How can this macro perform the function described?

Sub ExtrctMonthlySum()
'
' ExtrctMonthlySum Macro
'

'
    ActiveWorkbook.Queries.Add Name:="Table002 (Page 1)", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Pdf.Tables(File.Contents(""T:\Traverse\Church International & Triumphant\DCTC\Financial\Treasurer\2024\Acct 1893\2.pdf""), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & "    Table001 = Source{[Id=""Table001""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Table002,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
    ActiveWorkbook.Worksheets.Add
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table002 (Page 1)"";Extended Properties=""""" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [Table002 (Page 1)]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Table002__Page_1"
        .Refresh BackgroundQuery:=False
    End With
    Range("C5").Select
End Sub

5 Replies

  • InThought's avatar
    InThought
    Copper Contributor

    Gave it a shot which ended up with no values transferred into the pivot table which included the options provided: the obvious being sum of Amount.  Acct 9542 column A should be the header and the corresponding values aligned in the date row (Sheets2). Not sure how to handle the blank cells that are linked to the beginning and ending data.

    • Riny_van_Eekelen's avatar
      Riny_van_Eekelen
      Platinum Contributor

      Well, I can't check the queries as they connect to your local system, but I assume that the table in sheet "Acct 9542" represents the end result. Note that the 'Amount' column contains texts. In PQ, remove the $ sign and then set the data type to decimal number. Load to Excel and the pivot table will work.

       

  • InThought's avatar
    InThought
    Copper Contributor

    Verson: 365, PC, Will look into how to accomplish using PQ. Best regards

  • Riny_van_Eekelen's avatar
    Riny_van_Eekelen
    Platinum Contributor

    What excel version are you on? And what platform? Why not use Power Query directly? Writing queries via VBA isn't the most user friendly way, I think.

Resources