VBA excelmacros打开txt文件浏览

嗨,我想打开一个txt文件,但它每个月都在变化,所以我需要能够select新的浏览地图。

我是一个完整的VBA初学者,我logging了macros,但是在具体的编码部分,我并不知道大多数东西。

Sub Medical_txt_excel() With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\Users\user101\Documents\Macro Sales Monthly\Dec 2016-selected\Claim Medical.txt" _ , Destination:=Range("$A$10")) .Name = "Claim Medical" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False 

我需要索赔Medical.txt是一个文件,我可以select自己在使用macros而不必每次更改源代码

 ChDir "C:\Users\user101\Documents\Macro Sales Monthly\Dec 2016-selected" Dim fpath: fPath = Application.GetOpenFilename("Text Files (*.txt),*.txt") if fPath = False Then Exit Sub With ActiveSheet.QueryTables.Add(Connection:= "TEXT;" & fPath, Destination:=Range("A10")) ... End With 

尝试这个

 Sub Medical_txt_excel() Dim fd As Office.FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) fd.AllowMultiSelect = False fd.Title = "Please select the file." fd.Show With ActiveSheet.QueryTables.Add(Connection:= _ fd.SelectedItems(1) _ , Destination:=Range("$A$10")) .Name = "Claim Medical" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False End With End Sub