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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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,214,833
Messages
6,121,858
Members
449,051
Latest member
excelquestion515

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