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.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

BROWSE ALL TEMPLATES

50+ Project Management Templates Pack
Excel PowerPoint Word

VIEW DETAILS

Advanced Project Plan & Portfolio Template
Excel Template

VIEW DETAILS

Ultimate Project Management Template
Excel Template

VIEW DETAILS

20+ Excel Project Management Pack
Excel Templates

VIEW DETAILS

20+ PowerPoint Project Management Pack
PowerPoint Templates

VIEW DETAILS

10+ MS Word Project Management Pack
Word Templates

VIEW DETAILS


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
LIMITED TIME OFFER - Get it Now!
Advanced Project Plan Excel Template

 
Related Resource External VBA Reference