繁体   English   中英

Vba excel宏打开txt文件浏览

[英]Vba excel macro open txt file browse

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

我是一个完整的 VBA 初学者,我记录了宏,但是在进入特定的编码部分时,我真的不知道大多数东西。

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

我需要 Claim Medical.txt 是一个文件,我可以在使用宏时选择自己,而无需每次都更改源代码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM