Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Import

Status
Not open for further replies.

jeff1

IS-IT--Management
Nov 8, 2000
49
US
I have recorded a simple macro in excel that imports a space delimited text file and places the text in a pre-determined range of cells. I would like to know how to modify the macro to allow me to browse for the file each time the macro is run because the file name will not always be the same.

Thanks in advance
Jeff
 
Did you ever find a solution? I am currently needing the same thing. Chandler
I ran over my dogma with karma!
 
Put this code at the beginning of your module:

fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")

This will generate an "OpenFile" dialogue box which will allow you to choose the file you wish to open. fileToOpen will then return the path and the filename of the file you've chosen. This can then be manipulated by the code that you've already written, subsituting fileToOpen where necessary.

Have a look at the help file to make sure that the above code is OK for you - there are issues to consider such as whether you'd want the capability to cancel the dialogue box, whether to allow multiple selections of files etc.

Bryan.
 
Bryan:

I expect you to hang around and help us out regularly; do you part. We have needed you here!

(Translation: I can learn so much from you, you just gotta stick around! One of my coworkers created some fairly lengthy code to do this, and I just KNEW I'd seen it before and it had been just a couple lines--as you indicate. And no, his did not include "error handling". Thanks!) dreamboat@nni.com
Brainbench MVP for Microsoft Word
 
Just to clarify the error handling by the way, if the user presses cancel on the dialogue box, the fileToOpen will return a value of "False", which will cause problems if you don't incorporate a handling procedure into your macro.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top