Excel VBA / Refresh data from specific source without dialog

社会主义新天地 提交于 2020-01-06 09:08:07

问题


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:


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.



来源:https://stackoverflow.com/questions/51982136/excel-vba-refresh-data-from-specific-source-without-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!