I have successfully connected to a text file as per given code by using Excel's Get & Transform (Power Query) experience to connect to Text files.
Next base text file is Axis1.txt
which changes periodically.
Next I went to refresh the query table with latest data.
I referred following references to learn about refreshing querytables
How to reference and refresh a QueryTable in Excel 2016 in VBA
Excel VBA / Refresh data from specific source without dialog
https://learn.microsoft.com/en-us/office/vba/api/excel.querytable.refresh
Now my Problem .
Out of all referred methods I could get success in refreshing table with latest data when one of the query table cell is selected and the following command is given.
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Other types of code syntax refereed in various posts encounter an error mentioned against each line.
Sub reresh1()
'Sheet2.ListObject.QueryTable.Refresh BackgroundQuery:=False
'Sheets("Sheet2").QueryTables.Refresh BackgroundQuery:=False
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
'WORKS FINE
End Sub
Various other code syntax combinations were also tried but could not get success.
Sub TblImpV1()
' Macro1 Macro
ActiveWorkbook.Queries.Add Name:="axis1", Formula:= _but
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""C:\mydirb\axis1.txt""),[Delimiter="" "", Columns=11, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Column1"", Int64.Type}, {""Column2"", type date}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", type number}, {""C" & _
"olumn7"", type number}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", 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=axis1;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [axis1]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "axis1"
.Refresh BackgroundQuery:=False
End With
Range("B4").Select
End Sub
I Want to avoid Selection. Though procedure suggest that go to query table any cell and press refresh button. I believe SO Experts posts on related matter must be right and I am doing something silly, due to my elementary knowledge of VBA Coding. Any help or guidance in this matter is welcome. Would appreciate full related syntax instead of one liners.
EDIT
I could workout the solution to avoid the selection. Following code line works fine provided B2
is a cell in the Query Table.
Sheets("Sheet2").Range("B2").ListObject.QueryTable.Refresh BackgroundQuery:=False
However, If someone has a better solution , it is welcome.