Excel VBA / Refresh data from specific source with

2019-08-20 15:01发布

I have one sheet ("Sheet2") with imported data from TXT in my workbook. Data are refreshed every day from file ZLX02S_FG_ & "sufix". Name and path of the file are always known or calculated.

File name = ZLX02S_FG_20180821_095910.txt
Path name = C:\Users\lmisek\Desktop\WMS-L05-FG\

I have tried this code:

Sub Refresh_Macro()

    With Worksheets("Sheet2").QueryTables(1)
        .Connection = "TEXT;C:\Users\lmisek\Desktop\WMS-L05-FG\ZLX02S_FG_20180821_095910.txt"
        .Refresh BackgroundQuery:=False
     End With

End Sub

Data on the sheet2 are refreshed, but only if one will choose file in the dialog. I want to refresh them without dialog.

Any ideas?

1条回答
小情绪 Triste *
2楼-- · 2019-08-20 15:28

TextFilePromptOnRefresh property is what you are looking for.

With Worksheets("Sheet2").QueryTables(1)
   .TextFilePromptOnRefresh = False
   .Connection = "TEXT;C:\Users\lmisek\Desktop\WMS-L05-FG\ZLX02S_FG_20180821_095910.txt"
   .Refresh BackgroundQuery:=False
End With

Please note, that it may also be necessary to explicitly set in code some of the import parameters, like delimiter etc.

查看更多
登录 后发表回答