Display an OpenFile Dialog Box
Printer Friendly Page
The following code can be used to display an OpenFile dialog box.
This allows the user to select the workbook to be processed.
Dim filex As String
Dim wkb As Workbook
filex = Application.GetOpenFileName __
(FileFilter:="microsoft excel files (*.xls), *.xls", __
Title:="Get File", MultiSelect:=True)
If filex <> false then
Application.Workbooks.Open filex
Set wkb = ActiveWorkbook
mname = DIR(filex)
wkb.SaveAs filename:=hpath + mname
End if
Application.GetOpenFileName will display an open dialog box.
FILEX is a variable that will contain the name and path of the selected file.
If a file was not selected, FILEX will be false.
DIR(filex) will return the file name portion of the file in filex.
FileFilters describe which files are displayed in the Open File dialog
Examples of Filter Statements
FileFilter:="Microsoft Excel Files (*.xls), *.xls"
FileFilter:="Text Files (*.txt), *.txt"
FileFilter:="Add-In Files(*.xla), *.xla"
FileFilter:="Text Files (*.txt), *.txt", Add-In Files(*.xla), *.xla"
The above will list both filters in the drop-down box
FileFilter:="Visual Basic Files(*.bas; *.txt), *.bas; *.txt"
Semi-colons separate multiple file types for a single filter type