MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Loading a file into excel by macro


Posted by Michael Katz on September 02, 2001 7:35 AM

I need to enter a txt file into my excel sheet. The txt file contains data, but can be in any number of locations. Is there a way to write a macro that prompts the user for the file name and location?

Thanks in advance,

Mike


Posted by Dax on September 02, 2001 11:42 AM

You can use the GetOpenFileName method of the Excel application. E.g.

Sub PromptForATextFile()
Dim sTextFilePath As String

sTextFilePath = Application.GetOpenFilename("Text Files,*.txt", , "Open text file")
If sTextFilePath = "FALSE" Then Exit Sub 'User pressed Cancel

Workbooks.Open sTextFilePath
End Sub

HTH,
Dax.