VBA Open File Dialog Box helps to browse a file and open Excel Workbook. Users can choose file by clicking on the button to open an Excel File with specific filter, title.
VBA Code to Open an Excel File using File Dialog Box
We can open the Excel files using File dialog box using Application.GetOpenFilename method in VBA. The following are VBA Syntax and Examples to Open Excel Files using File Dialog Box.
VBA Code to Open an Excel File using File Dialog Box: Syntax
Here is the VBA code syntax to show the open dialog Box in Excel. Here we can specify the file filter, File dialog title to show.
strFileToOpen = Application.GetOpenFilename _
(Title:="Please choose a file to open", _
FileFilter:="Excel Files *.xls* (*.xls*),")
VBA Code to Open an Excel File using File Dialog Box: Example
Here is the VBA Code to Open an Excel File using File Dialog Box. Here we first capture the file path using File Dialog Box and then we can open the File using Workbook. Open method.
Sub sbVBA_To_Open_Workbook_FileDialog() Dim strFileToOpen As String strFileToOpen = Application.GetOpenFilename _ (Title:="Please choose a file to open", _ FileFilter:="Excel Files *.xls* (*.xls*),") If strFileToOpen = False Then MsgBox "No file selected.", vbExclamation, "Sorry!" Exit Sub Else Workbooks.Open Filename:=strFileToOpen End If End Sub
Explained VBA Code to Open an Excel File using File Dialog Box
Here is the explaination to the above code.
‘Strating sub procedure to write VBA Code to Open an Excel File using File Dialog Box
Sub sbVBA_To_Open_Workbook_FileDialog_C()
‘Declaring a Variable to hold the file choosen using File DilogBox
Dim strFileToOpen As String
‘Choosing an Excel File using File dialog Box and capturing the file path in the variable
strFileToOpen = Application.GetOpenFilename _
(Title:=”Please select an Excel file to open”, _
FileFilter:=”Excel Files *.xls* (*.xls*),”)
‘Here you can note that we have allowed any excel files to choose
‘We have also customized the file dialog title
‘Checking if file is selected
If strFileToOpen = False Then
‘Displaying a message if file not choosedn in the above step
MsgBox “No file selected.”, vbExclamation, “Sorry!”
‘And existing from the procedure
Exit Sub
Else
‘Openning the file if selected in the above step
Workbooks.Open Filename:=strFileToOpen
End If
‘Ending the sub procedure
End Sub
VBA Code to Open Only (.xls) Excel 2003 format Files using File Dialog Box
You can filter the files to choose and open specific files with required file extensions. The below VBA code example will show you how to filter the excel files and allow the user to choose only .xls files.
'Strating sub procedure to write VBA Code to Open an only Excel 2003 Files using File Dialog Box Sub sbVBA_To_Open_Workbook_FileDialog_xls_C() 'Declaring a Variable to hold the file choosen using File DilogBox Dim strFileToOpen As String 'Choosing an Excel File using File dialog Box and capturing the file path in the variable strFileToOpen = Application.GetOpenFilename _ (Title:="Please select an Excel file to open", _ FileFilter:="Excel Files *.xls (*.xls),") 'Here you can note that we have allowed any (.xls) excel files to choose 'We have also customized the file dialog title 'Checking if file is selected If strFileToOpen = False Then 'Displaying a message if file not choosedn in the above step MsgBox "No file selected.", vbExclamation, "Sorry!" 'And existing from the procedure Exit Sub Else 'Openning the file if selected in the above step Workbooks.Open Filename:=strFileToOpen End If 'Ending the sub procedure End Sub
VBA Code to Open Only (.xlsm) Excel 2007+ format Macro Files using File Dialog Box
You can restric the users to choose specific files with required file extensions. The below VBA code example will show you how to filter the excel files and allow the user to choose only 2007 -2013 excel macro files (.xlsm) files.
'Strating sub procedure to write VBA Code to Open an only Excel 2007 macro Files using File Dialog Box Sub sbVBA_To_Open_Workbook_FileDialog_xlsm_C() 'Declaring a Variable to hold the file choosen using File DilogBox Dim strFileToOpen As String 'Choosing an Excel File using File dialog Box and capturing the file path in the variable strFileToOpen = Application.GetOpenFilename _ (Title:="Please select an Excel file to open", _ FileFilter:="Excel Files *.xlsm (*.xlsm),") 'Here you can note that we have allowed any (.xlsm) excel macro files to choose 'We have also customized the file dialog title 'Checking if file is selected If strFileToOpen = False Then 'Displaying a message if file not choosedn in the above step MsgBox "No file selected.", vbExclamation, "Sorry!" 'And existing from the procedure Exit Sub Else 'Openning the file if selected in the above step Workbooks.Open Filename:=strFileToOpen End If 'Ending the sub procedure End Sub
Is not supposed to be “False” (with quotes) instead of False (without quotes)?
By the way, thank you very much for the code and the explanation.
Hi ,
It shud be “If strFileToOpen = ” “and not “If strFileToOpen = False”.
Regards,
Mahantesh
This worked for me:
If strFileToOpen = “False” Then
No file was selected, so strFileToOpen returns a value of False.
However you’ve initialized the variable as a string, so you must put the return value in quotes or you get a type mismatch error.
it doesn’t work for me it gives me this error :
execution error 428, object not found
Hi Saadia, pls. replace the following code
If strFileToOpen = False Then
With
If strFileToOpen = ” Then
If strFileToOpen = Null
I too have had an issue with the sample code working properly. I think the issue is definition of the variable strFileToOpen. The sample code shows it to be of type String. It should be of type Variant. That way “False” works when no file is selected (i.e. selecting Cancel in the dialog box) and yet will open the file properly when a file is selected.
Hello All
I want to know how to get the file name. As I wanted to expand a macro little bit so need your help guys. I want to get the file name ( which will be changing as it is dependent on the user which file he selects) so that I can copy the data from it and paste it into my dashboard file.
Thanks In Advance.
Regards
Tushar Rawat
I used :
Dim strFileToOpen as variante
….
If strFileToOpen = False then
….
All the rest the same.
It worked for me
Good luck!
I want to know how to get the file name. As I wanted to expand a macro little bit so need your help guys. I want to get the file name ( which will be changing as it is dependent on the user which file he selects) .
how to switch files after open the files with this option