2

Hi i want to open a txt file but it changes every month so i need to be able to select the new one browse through the map.

I am a complete beginner with VBA and i recorded the macro but when going in the specific coding part I don't really know most stuff.

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

i need the Claim Medical.txt to be a file i can select myself when using the macro without changing the source code every time

R3uK
  • 14,417
  • 7
  • 43
  • 77
M.P
  • 23
  • 5

2 Answers2

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
A.S.H
  • 29,101
  • 5
  • 23
  • 50
0

Try this

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
Aeneas
  • 270
  • 1
  • 7