REAL-TIME

VBA Projects

Full Access with Source Code
  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

Effortlessly
Manage Your Projects

120+ Project Management Templates

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Share Post

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 open file dilog 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
Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBATags: , Last Updated: June 17, 2022

11 Comments

  1. Fredy Armenta August 29, 2014 at 3:20 AM

    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.

  2. Mahantesh December 5, 2014 at 10:28 AM

    Hi ,

    It shud be “If strFileToOpen = ” “and not “If strFileToOpen = False”.

    Regards,
    Mahantesh

  3. Mike March 9, 2015 at 9:29 PM

    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.

  4. Saadia March 25, 2015 at 9:19 PM

    it doesn’t work for me it gives me this error :
    execution error 428, object not found

  5. Sylvester August 6, 2015 at 10:06 PM

    Hi Saadia, pls. replace the following code

    If strFileToOpen = False Then

    With
    If strFileToOpen = ” Then

  6. Jin April 7, 2016 at 4:42 PM

    If strFileToOpen = Null

  7. Brian January 4, 2017 at 7:37 PM

    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.

  8. Tushar Rawat February 22, 2017 at 11:38 AM

    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

  9. Adi October 5, 2017 at 3:05 PM

    I used :
    Dim strFileToOpen as variante
    ….
    If strFileToOpen = False then
    ….

    All the rest the same.

    It worked for me

    Good luck!

  10. Abhilash February 7, 2018 at 4:45 PM

    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) .

  11. jagan December 24, 2018 at 3:34 PM

    how to switch files after open the files with this option

Leave A Comment