Code:
Sub ImportaDati() Dim qr As Queries
' ImportaDati Macro
Dim FL As String
Dim Num As Integer
Dim qry As WorkbookQuery 'elimino le connessioni precedenti
For Each qry In ThisWorkbook.Queries
qry.Delete
Next qry
FL = Range("L1")
If Range("L3") = "autostrade" Then
ActiveWorkbook.Queries.Add Name:="Table006 (Page 2) (2)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Origine = Pdf.Tables(File.Contents(""C:\Users\" & Environ("username") & "\OneDrive - Indipendente\Fisco\telepass\tele\" & FL & """), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & " Table006 = Origine{[Id=""Table006""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Intestazioni alzate di livello"" = Table.PromoteHeaders(Table006, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Modificato tipo"" = Table.Tra" & _
"nsformColumnTypes(#""Intestazioni alzate di livello"",{{""DATA"", type text}, {""ORA USC."", type text}, {""SRV"", type text}, {""DESCRIZIONE"", type text}, {""Column5"", type text}, {""CLASSE"", type text}, {""IMPORTO"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Modificato tipo"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table006 (Page 2) (2)"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table006 (Page 2) (2) ]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table006__Page_2"
.Refresh BackgroundQuery:=False
End With
On Error GoTo Extsub ' vedo se c'è anche un'altra tabella che si chiama table 007 e viene dopo la principale che ho appena importato sopra
ActiveWorkbook.Queries.Add Name:="Table007 (Page 2) (2)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Origine = Pdf.Tables(File.Contents(""C:\Users\" & Environ("username") & "\OneDrive - Indipendente\Fisco\telepass\tele\" & FL & """), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & " Table006 = Origine{[Id=""Table006""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Intestazioni alzate di livello"" = Table.PromoteHeaders(Table006, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Modificato tipo"" = Table.Tra" & _
"nsformColumnTypes(#""Intestazioni alzate di livello"",{{""DATA"", type text}, {""ORA USC."", type text}, {""SRV"", type text}, {""DESCRIZIONE"", type text}, {""Column5"", type text}, {""CLASSE"", type text}, {""IMPORTO"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Modificato tipo"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table006 (Page 2) (2)"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table007 (Page 2) (2)]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table007__Page_2"
.Refresh BackgroundQuery:=False
End With
ElseIf Range("L3") = "parcheggi" Then
ActiveWorkbook.Queries.Add Name:="Table006 (Page 2) (2)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Origine = Pdf.Tables(File.Contents(""C:\Users\" & Environ("username") & "\OneDrive - Indipendente\Fisco\telepass\tele\" & FL & """), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & " Table001 = Origine{[Id=""Table001""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Intestazioni alzate di livello"" = Table.PromoteHeaders(Table001, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Modificato tipo"" = Table.Tran" & _
"sformColumnTypes(#""Intestazioni alzate di livello"",{{""DATA/ORA SOSTA"", type text}, {""DESCRIZIONE"", type text}, {""Column3"", type text}, {""IMPORTO"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Modificato tipo"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table001 (Page 1)"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table006 (Page 2)]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table006__Page_2"
.Refresh BackgroundQuery:=False
End With
End If