funzione accodare query Power Query

Anonimo
2022-05-28T11:22:42+00:00

buon giorno, mi presento qui per chiedere un vostro aiuto in un problemino che ho con vba su excel 365 professional. spiego brevemente, devo importare in una cartella delle tabelle da uno o due file pdf. il problema che ho, consiste nel fatto che i pdf hanno numeri di pagine variabili, da 5 sino anche 500. quello che riesco a fare è caricare i file i query, e per unirli ho dovuto ripetere la formula accoda in base a quante pagine ci sono. non riesco a dirgli di capire da solo quante sono le pagine e di accodarle....

Sub Macro_carica_accorpa_1()

'

Dim wbk3 As Workbook

Dim Indirizzo As String

Dim Indirizzo1 As String

Dim COUNT As Integer

Dim COUNT1 As Integer

Dim DATA As String

Dim sh As Worksheet

Dim ListObj As ListObject

Dim pq As Object

Application.ScreenUpdating = False

Application.DisplayAlerts = False

Indirizzo = Range("u1").Value

PageN = Range("w1").Value

Pages = Range("w2").Value

Indirizzo1 = Range("u2").Value

PageT = Range("w3").Value

Do

COUNT = COUNT + 1 

PageRif = "Page" & Format(COUNT, "000") 

ActiveWorkbook.Queries.Add Name:="Page" & Format(COUNT, "000"), Formula:= \_ 

    "let" & Chr(13) & "" & Chr(10) & "    Origine = Pdf.Tables(File.Contents(""" & Indirizzo & """), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & "    Page1 = Origine{[Id=""" & PageRif & """]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Modificato tipo"" = Table.TransformColumnTypes(Page1,{{""Column1"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " " & \_ 

    "   #""Modificato tipo""" 

Loop While COUNT < PageN

If Pages = 0 Then

Else

Do

COUNT = COUNT + 1 

COUNT1 = COUNT1 + 1 

PageRif = "Page" & Format(COUNT1, "000") 

ActiveWorkbook.Queries.Add Name:="Page" & Format(COUNT, "000"), Formula:= \_ 

    "let" & Chr(13) & "" & Chr(10) & "    Origine = Pdf.Tables(File.Contents(""" & Indirizzo1 & """), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & "    Page1 = Origine{[Id=""" & PageRif & """]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Modificato tipo"" = Table.TransformColumnTypes(Page1,{{""Column1"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " " & \_ 

    "   #""Modificato tipo""" 

Loop While COUNT < PageT

End If

If Sheets("Comandi").Range("w3").Value = 1 Then

Sheets("Comandi").Range("w4").Value = 1

Else

End If 

If Sheets("Comandi").Range("w3").Value = 2 Then

ActiveWorkbook.Queries.Add Name:="Accoda1", Formula:= _

    "let" & Chr(13) & "" & Chr(10) & "    Origine = Table.Combine({Page001, Page002})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Origine" 

 Sheets("Comandi").Range("w4").Value = 2 

Else 

End If 

If Sheets("Comandi").Range("w3").Value = 3 Then 

ActiveWorkbook.Queries.Add Name:="Accoda1", Formula:= _

    "let" & Chr(13) & "" & Chr(10) & "    Origine = Table.Combine({Page001, Page002, Page003})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Origine" 

Sheets("Comandi").Range("w4").Value = 3 

Else 

End If 

If Sheets("Comandi").Range("w3").Value = 4 Then 

ActiveWorkbook.Queries.Add Name:="Accoda1", Formula:= _

    "let" & Chr(13) & "" & Chr(10) & "    Origine = Table.Combine({Page001, Page002, Page003, Page004})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Origine" 

Sheets("Comandi").Range("w4").Value = 4 

Else 

End If 

If Sheets("Comandi").Range("w3").Value = 5 Then 

ActiveWorkbook.Queries.Add Name:="Accoda1", Formula:= _

    "let" & Chr(13) & "" & Chr(10) & "    Origine = Table.Combine({Page001, Page002, Page003, Page004, Page005})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Origine" 

Sheets("Comandi").Range("w4").Value = 5 

Else 

End If 

If Sheets("Comandi").Range("w3").Value = 6 Then 

ActiveWorkbook.Queries.Add Name:="Accoda1", Formula:= _

    "let" & Chr(13) & "" & Chr(10) & "    Origine = Table.Combine({Page001, Page002, Page003, Page004, Page005, Page006})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Origine" 

Sheets("Comandi").Range("w4").Value = 6 

Else 

End If 

If Sheets("Comandi").Range("w3").Value = 7 Then 

ActiveWorkbook.Queries.Add Name:="Accoda1", Formula:= _

    "let" & Chr(13) & "" & Chr(10) & "    Origine = Table.Combine({Page001, Page002, Page003, Page004, Page005, Page006, Page007})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Origine" 

Sheets("Comandi").Range("w4").Value = 7 

Else 

End If

ecc...(io sono arrivato a scrivere sino a 70 con le possibilità di accodamento)

If Sheets("Comandi").Range("w4").Value = 1 Then

ActiveWorkbook.Worksheets.Add 

With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= \_ 

    "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & PageRif & ";Extended Properties=""""" \_ 

    , Destination:=Range("$A$1")).QueryTable 

    .CommandType = xlCmdSql 

    .CommandText = Array("SELECT \* FROM [Page" & Format(COUNT, "000") & "]") 

    .RowNumbers = False 

    .FillAdjacentFormulas = False 

    .PreserveFormatting = True 

    .RefreshOnFileOpen = False 

    .BackgroundQuery = True 

    .RefreshStyle = xlInsertDeleteCells 

    .SavePassword = False 

    .SaveData = True 

    .AdjustColumnWidth = True 

    .RefreshPeriod = 0 

    .PreserveColumnInfo = True 

    .ListObject.DisplayName = "Page" & Format(COUNT, "000") 

    .Refresh BackgroundQuery:=False 

    Application.DisplayAlerts = False 

End With 

Else 

ActiveWorkbook.Worksheets.Add 

With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= \_ 

    "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Accoda1;Extended Properties=""""" \_ 

    , Destination:=Range("$A$1")).QueryTable 

    .CommandType = xlCmdSql 

    .CommandText = Array("SELECT \* FROM [Accoda1]") 

    .RowNumbers = False 

    .FillAdjacentFormulas = False 

    .PreserveFormatting = True 

    .RefreshOnFileOpen = False 

    .BackgroundQuery = True 

    .RefreshStyle = xlInsertDeleteCells 

    .SavePassword = False 

    .SaveData = True 

    .AdjustColumnWidth = True 

    .RefreshPeriod = 0 

    .PreserveColumnInfo = True 

    .ListObject.DisplayName = "Accoda1" 

    .Refresh BackgroundQuery:=False 

End With 

End If 

End Sub 

la formula funziona(sino alle 70 pagine fa egregiamente il suo lavoro) , ma siccome posso avere il pdf che può arrivare sino a 500 pagine e oltre, o trovo un modo per caricare tutto il pdf in una unica tabella(visto che le pagine hanno la stessa intestazione) o un modo per automatizzare magari con do e loop la parte che riguarda l'accodamento....

ActiveWorkbook.Queries.Add Name:="Accoda1", Formula:= _

    "let" & Chr(13) & "" & Chr(10) & "    Origine = Table.Combine({Page001, Page002, Page003, Page004, Page005})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Origine" 

ma non riesco a capire come dargli i riferimenti.

vi ringrazio per qualsiasi mano che mi possiate dare.

Microsoft 365 e Office | Excel | Per il lavoro | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento

6 risposte

Ordina per: Più utili
  1. Anonimo
    2022-05-31T16:30:53+00:00

    Ciao Fabio,

    scusa, ma non riesco ad integrare la tua con la mia.... perchè la tua è settata su una sola colonna, e non riesco a sbloccarla(il pezzo che ho io si adatta in automatico al numero di colonne, che nel mio caso possono essere 16 o 18)... non sono bravo con il vba(credo si capisca)... :-(

    Se crei una query basata su pagine anziché su tabelle e consenti il numero massimo possibile di colonne PDF, la query può essere semplicemente aggiornata, manualmente o con una macro a riga singola.

    La query può essere creata con VBA o manualmente. Se desideri crearlo manualmente, ti fornirò istruzioni dettagliate passo passo. In entrambi i casi, la query può essere riutilizzata o persino copiata tra cartelle di lavoro.

    Ci sono due problemi minori:

    • Dato il numero di colonne pdf, alcune di queste colonne verranno riportate sotto la tabella contenente (diciamo) le prime 12 colonne;
    • Se il file pdf di interesse non ha un nome costante, sarà necessario rivedere il linguaggio M della formula della query.

    Questi, tuttavia, sono problemi tecnici minori che possono essere facilmente superati.

    Quindi, se ti interessa caricare un file pdf di esempio, comprendente il numero massimo di colonne e confermare la domanda sul nome del file pdf, creerò volentieri una soluzione completa.

    ===

    Regards,

    Norman

    Immagine

    La risposta è stata utile?

    0 commenti Nessun commento