Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Opening new workbooks in excel 1

Status
Not open for further replies.

ab123456

Chemical
Mar 18, 2003
58
I am trying to open a new file within excel when running a macro. The file to open will have a different name each time so i require the user to input the filename. The file will always be a comma separated text file. I need the file to open in excel without having to go through the text import wizard each time.

I have got the code to automatically open the CSV if i know the filename but am having problems in allowing the user to select the file.

I would like to use the standard open file dialog box to do this. I can show the dialog using the code
Application.Dialogs(xlDialogOpen).Show
and this works fine except that once the user selects the file excel tries to open it and launches the text import wizard.

Is there a way that i can use the standard dialog to return the filename but not open it? I can then use the code below to avoid the text import wizard running

Workbooks.OpenText Filename:= name_and_path_of_file, StartRow:=1, DataType:=xlDelimited, comma:=True
 
Replies continue below

Recommended for you

Use the GetOpenFilename method to raise a dialogue that will return the file name but not open the file. Once you have the file name use the WorkBooks.OpenText method to open the text file
 
Expanding on cummings54's reply, here is some example code:
Code:
Sub GetFile()
Dim FPath As Variant
Dim FFilter As String

   FFilter = "Comma Delimited (*.csv),*.csv"
   FPath = Application.GetOpenFilename(FFilter, , "Select File To Open", "Open")
   If FPath = False Then Exit Sub
   'FPath contains path & filename of user-selected file
   
   'Your existing code here
   
End Sub


Regards,
Mike

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor