If I am not allowed to post this here - apols in advance and please just let me know but this forum has been sorting things out for me for years
Is it possible to create a loop that will go through a list of Connection Only PQs, adding them as tables?
If I record a single macro, of me manually doing this, it does work, which is unreal.
However I have a list of say 10 queries and I would like it to do it for me.
This is attempt, which creates the new sheet, and then creates a Table ref, dies on the highlighted line and the data doesn't refresh
Sub LoopThruPQsMakingTables()
'declare a variant array
Dim strNames(1 To 2) As String
'populate the array
strNames(1) = "Table_1"
strNames(2) = "Table_2"
'declare a variant to hold the array element
Dim item As Variant
'loop through the entire array
For Each item In strNames
Sheets.Add After:=ActiveSheet
Debug.Print item
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=item;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM item ")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.ListObject.DisplayName = item
.Refresh BackgroundQuery:=False ' this is where it appears to error and it also doesn't loop to second array item which I guess is logical
'.ListObject.QueryTable.Refresh BackgroundQuery:=False ' tried this also but didn't work
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=True
Next item
End Sub
Any hints ?