2017-02-07 3 views
2

こんにちは、私はtxtファイルを開きたいですが、それは毎月変わるので、私はマップを使って新しいものを選択する必要があります。Vba excelマクロオープンtxtファイルブラウズ

私はVBAの完全な初心者です。マクロを記録しましたが、特定のコーディング部分に入ると、ほとんどのものは実際には分かりません。ソースコードを変更せずに

私は、請求項Medical.txtマクロを使用しているとき、私は自分自身を選択することができますファイルである必要が
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 

たび

答えて

1
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 
+1

はありがとう試してみてくださいこのコードは、私が望むものに完璧に働いていました! –

+0

@ M.Pようこそ、喜んで:) –

0

この

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