import a text file with user input

Corbana

New Member
Joined
Nov 9, 2005
Messages
23
i want to import a text file with the user choosing twhere the file comes from not just having to rename the file they want to import

Code:
'
With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\Jim\import.txt" _
        , Destination:=Sheets("Sheet2").Range("A1"))

is what a record gives me but i want to change this so that the user can choose the name of the file or prefrably get the chance to navigate to it.

ta
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Code:
Sub test()
Dim txtfile As Variant

With Application.FileDialog(msoFileDialogFilePicker)
    .Filters.Add "Text files", "*.txt", 1 'default to show only txt files
    
    If .Show = -1 Then 'show file select dialog
        For Each txtfile In .SelectedItems
            'code to import selected text file
            With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & txtfile, Destination:=Sheets("Sheet2").Range("A1"))
            'any other code you want to run
        Next txtfile
    End If
End With

End Sub
 
Upvote 0
Oops! I forgot an End With in there :oops: I took that out of a project I just did at work, so apparently just missed it. Try:

Code:
Sub test() 
Dim txtfile As Variant 

With Application.FileDialog(msoFileDialogFilePicker) 
    .Filters.Add "Text files", "*.txt", 1 'default to show only txt files 
    
    If .Show = -1 Then 'show file select dialog 
        For Each txtfile In .SelectedItems 
            'code to import selected text file 
            With ActiveSheet.QueryTables.Add(Connection:= _ 
            "TEXT;" & txtfile, Destination:=Sheets("Sheet2").Range("A1")) 
                 'any other code you want to run
            End With
        Next txtfile 
    End If 
End With 

End Sub
 
Upvote 0

Forum statistics

Threads
1,222,032
Messages
6,163,511
Members
451,839
Latest member
HonestZed

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top