Discussion Locked

Excel: how to get macro to pause for fil

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I wish to create a macro that includes opening a file. The macro recorder will do this but hard codes the particular file name in the code. I want the macro to pause and give me the option to select the file to be opened, and then continue after I have selected. I used to be able to do this with Lotus so am sure that Excel can too.

7 replies

by a.douwes

Just before you are ready to call the openfile function create a "expression.InputBox(Prompt, Title, Default, Left, Top, HelpFile, HelpContextId, Type)" (source: VBAXL8.HLP file) And ask for the file name. Store the response in a variable and pass that along to the Open function. More information can be found in the VBAXL8.HLP file on the OFFICE 97 CDROM under \office folder and probably on your hard drive under \PROGRAM FILES\MIROSOFT OFFICE\OFFICE.

by mike_molloy

There is a method called GETOPENFILENAME that you can use to present the file open dialog box to the users and let them pick a file to open. It doesn't actually open the file, you have to use a second statement to do that. This statement will put the name in a variable, and you use the variable in the file open statement.

You can apply a file file (*.xls, etc)if you wish, and you have a couple of other option that are described in the vba help item for the method.

by bdeitrick

Here's a little sub that shows you how to get a filename and open that file:

Sub PromptToOpenFile()
Application.Workbooks.Open Application.GetOpenFilename
End Sub

Bernie

This discussion is closed to new replies.