Workbooks.Open "C:ReportsSales.xlsx" opens a workbook from its full path, including the file extension. To work with it afterwards, store it in a variable: Set wb = Workbooks.Open("C:ReportsSales.xlsx"), then use wb.Worksheets("Data").Range("A1").Value.
Add named arguments for the common options: ReadOnly:=True to open it read-only, Password:="open123" for a file that needs a password to open, and UpdateLinks:=0 to skip the prompt about updating links to other workbooks. For a file in the same folder as the one holding the code, build the path with ThisWorkbook.Path & "Sales.xlsx".
To let the user pick the file, use GetOpenFilename, which returns False when they click Cancel:
Dim f As Variant
f = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*")
If f = False Then Exit Sub
Workbooks.Open f
Check the file exists before opening it, with If Dir(path) = "" Then, because a wrong path stops the code with error 1004. When looping through a folder with Dir, end the folder path with a backslash, and skip names that start with ~$; those are lock files Excel creates for workbooks that are already open.
To open a workbook using VBA, you need to use the “Workbooks.Open” method and specify the path of the file (make sure to specify the full path to the workbook with the name and file extension). This method has a total of fifteen arguments; only the first one (FileName) is required, and you can use the optional ones to deal with different kinds of files.
In this tutorial, we will explore it in detail and look at an alternative method that you can use.
What is Workbooks.Open
In VBA, the Workbooks.Open method is a part of the Workbooks collection object. It can be used to open a workbook in Excel from within another Excel file or a VBA project. The Workbooks.Open method does not directly refer to an object but rather to an action performed by the Workbooks collection object, which is part of the Excel VBA model.
When you use the Workbooks.Open method, it returns a Workbook object that represents the workbook you’ve just opened.
This allows you to change the opened workbook, such as reading or writing to cells, adding formulas, modifying styles, and more.
Workbooks.Open Syntax
Now it’s time to look at the syntax of this method. As I mentioned, there are fifteen arguments that you can use:
expression.Open (FileName, UpdateLinks, _ ReadOnly, Format, Password, WriteResPassword, IgnoreReadOnlyRecommended, Origin, _ Delimiter, Editable, Notify, Converter, AddToMru, Local, CorruptLoad)
Arguments
- Filename: Name of the file to be opened.
- UpdateLinks: Specifies how links in the file are updated. If this argument is omitted, the user is prompted to specify how links will be updated.
- ReadOnly: True opens the workbook in read-only mode.
- Format: If Microsoft Excel opens a text file, this argument specifies the delimiter character.
- Password: A String that’s the password required to open a protected workbook.
- WriteResPassword: A String that’s the password required to write to a write-reserved workbook.
- IgnoreReadOnlyRecommended: True to have Excel not display the read-only recommended message.
- Origin: If the file is a text file, this argument specifies the origin of the text file.
- Delimiter: If the file is a text file and the Format argument is 6, this argument is a string that specifies the character used as the delimiter.
- Editable: If the file is an Excel template, True to open the specified template for editing.
- Notify: If the file can’t be opened in read/write mode (for example, someone else has it open), True adds the file to the file notification list, so Excel opens it as read-only and lets you know when it becomes available.
- Converter: The index of the first file converter to try when opening the file. The specified file converter is tried first; if this converter doesn’t recognize the file, all other converters are tried.
- AddToMru: True to add this workbook to the list of recently used files.
- Local: TRUE saves files against the language of Microsoft Excel (including control panel settings).
- CorruptLoad: This can be one of the following constants: xlNormalLoad, xlRepairFile, or xlExtractData. These constants specify how to open the workbook if it’s corrupted.
Steps to Open a Workbook using VBA
As I have mentioned, to open a workbook in Excel using VBA, you can use the Workbooks.Open method.

- Open Excel and press Alt + F11 to open the VBA Editor.
- In the VBA Editor, insert a new module by right-clicking on any of the objects in the Project window, then choose Insert > Module.
- In the module window, start the code with the “Workbooks” object. Type a dot (.) after that and select the Open method from the list.
- Specify the file path in the first argument and make sure to enclose it in double quotation marks. In the end, run the code to open the workbook.
Sub vba_open_workbook() Workbooks.Open "C:\Users\Dell\Desktop\myFile.xlsx" End Sub
You need to replace “C:\Users\Dell\Desktop\myFile.xlsx” with the actual file path of the workbook you wish to open.
The path must be a string that represents the full path to the file.
Helpful Links: Run a Macro – Macro Recorder – Visual Basic Editor – Personal Macro Workbook
You won’t be using all these arguments. But a few of them are quite important and could be useful for you in the real world.
Opening a Password Protected Workbook
If you want to open a workbook that is password-protected, you can specify the password with the Password argument.
Here I have a workbook on the desktop that has the password “test123”, and now I want to open it and unprotect it at the same time. Following is the code that I need to use.
Workbooks.Open "C:\Users\Dell\Desktop\myFile.xlsx", , , Password:="test123"
The two commas skip the UpdateLinks and ReadOnly arguments. You can also write it only with named arguments: Workbooks.Open Filename:=”C:\Users\Dell\Desktop\myFile.xlsx”, Password:=”test123″. Keep in mind that a password in the code can be read by anyone who opens the VBA editor.
Opening a Workbook as Read Only
To open a workbook as read-only using VBA, you can utilize the “Workbooks.Open” method with the ReadOnly argument set to TRUE.
This ensures that the workbook is opened in a mode that prevents users from making changes to the original file.
Sub OpenWorkbookAsReadOnly() Dim filePath As String ' Specify the full path to the workbook you want to open filePath = "C:\Users\Dell\Desktop\myFile.xlsx" ' Open the workbook as read-only Workbooks.Open Filename:=filePath, ReadOnly:=True End Sub
- filePath: Replace “C:\Users\Dell\Desktop\myFile.xlsx” with the actual full path to the workbook you wish to open.
- Workbooks.Open: This is the method used to open a workbook in Excel with VBA.
- Filename:=filePath: This argument specifies the path to the file you want to open.
- ReadOnly:=True: By setting the ReadOnly parameter to True, the workbook is opened in read-only mode, preventing any changes from being saved to the original file.
Open All the Workbooks from a Folder
To open all the workbooks located within a specific folder using VBA, you can use the Dir function in combination with a Do While Loop.
The Dir function is used to get the names of files and directories within a specified path.
Sub vba_open_multiple_workbooks_folder() Dim wb As Workbook Dim strFolder As String Dim strFile As String strFolder = "C:\Users\Dell\Desktop\folder\" strFile = Dir(strFolder & "*.xls*") Do While strFile <> "" Set wb = Workbooks.Open(strFolder & strFile) strFile = Dir Loop End Sub
To use it as per your needs, make sure to change the folder path, and keep the backslash at the end of it, as the code adds the file name right after the folder path.
Open a Workbook using File Dialog Box
Opening a workbook using a File Dialog box in VBA allows users to select the file they want to open.
Sub vba_open_dialog() Dim strFile As String strFile = Application.GetOpenFilename() If strFile = "False" Then Exit Sub ' Cancel was clicked Workbooks.Open (strFile) End Sub
- Dim strFile As String: This line declares a variable strFile as a string. This variable will be used to store the file path of the workbook to be opened.
- strFile = Application.GetOpenFilename(): This line assigns the file path of the selected file to the variable strFile. The GetOpenFilename method displays the standard Open dialog box and gets a file name from the user without actually opening any files. If the user clicks Cancel, it returns False, so the next line exits the code instead of trying to open a file called “False”.
- Workbooks.Open (strFile): This line opens the workbook at the file path stored in strFile. The Workbooks.Open method opens a workbook in Excel.
With the help of the Open File dialog box (once you learn to use it), you can write dynamic code.
Working with the Opened Workbook
After opening a workbook, you might want to perform operations on it, such as accessing a specific worksheet or range. You can do this by assigning the opened workbook to a Workbook variable:
Sub UseOpenedWorkbook() Dim myPath As String Dim myWB As Workbook ' Specify the path to the workbook you want to open myPath = "C:\YourPath\YourWorkbook.xlsx" ' Open the workbook and assign it to the variable Set myWB = Workbooks.Open(Filename:=myPath) ' Example operation: Activate a specific sheet myWB.Sheets("Sheet1").Activate End Sub
Opening All Excel Files from a Folder
If you want to open all Excel files within a specific folder, you can do this by looping through the folder contents.
Sub OpenAllWorkbooksFromFolder() Dim myPath As String Dim myFile As String Dim wb As Workbook ' Specify the folder path containing the Excel files myPath = "C:\Path\To\YourFolder\" ' Check for trailing backslash in folder path If Right(myPath, 1) <> "\" Then myPath = myPath & "\" ' Find the first Excel file in the folder myFile = Dir(myPath & "*.xls*") ' Loop through all Excel files in the folder Do While myFile <> "" ' Open the workbook Set wb = Workbooks.Open(myPath & myFile) ' Move to the next file (this line is essential to avoid an endless loop) myFile = Dir Loop End Sub
- myPath, myFile, and wb. myPath is a string variable that will hold the path to the folder where the Excel files are located. myFile is a string variable that will hold the name of each Excel file in the folder. wb is a Workbook object that will reference each opened Excel file.
- Next, myPath is assigned the path to the directory containing the Excel files. If the path does not end with a backslash, one is appended to it.
- The Dir function is then used to assign the name of the first Excel file in the folder to myFile. The *.xls* wildcard allows this function to find .xls, .xlsx and .xlsm file extensions.
- A loop is then started with Do While myFile <> “”. This loop will continue as long as myFile is not an empty string; an empty string means that there are no more Excel files in the folder.
- Inside the loop, the Workbooks.Open function is used to open each Excel file, and wb is assigned this opened Workbook object. The Dir function is then called again with no arguments to get the next Excel file in the folder, and myFile is updated to this new file name.
This loop will continue until all Excel files in the folder have been opened.
Opening Multiple Specific Files
This method is more suitable when you need to open multiple files, but those files are fixed.
Sub OpenMultipleWorkbooks() Dim filePaths As Variant Dim i As Integer ' List of file paths to open filePaths = Array("C:\Path\To\FirstWorkbook.xlsx", _ "C:\Path\To\SecondWorkbook.xlsx", _ "C:\Path\To\ThirdWorkbook.xlsx") ' Loop through the array and open each workbook For i = LBound(filePaths) To UBound(filePaths) Workbooks.Open filePaths(i) Next i End Sub
- Variable filePaths holds an array of file paths to the Excel files you want to open, and variable i will be used as a counter in the loop.
- The Array function is used to create an array of file paths that you want to open. In our example, three file paths are given, but you can add or remove as many as you need.
- The For loop then iterates over each element in the filePaths array. The LBound function returns the smallest subscript for the indicated dimension of an array, and the UBound function returns the largest subscript.
- In each iteration of the loop, the Workbooks.Open method is called with the current file path as an argument, opening each Excel file in turn.
- The Next i statement marks the end of the loop block and moves the loop to the next element in the filePaths array. The loop will continue until all Excel files specified in the filePaths array have been opened.
Check if the File Exists Before Opening It
If the path is wrong or the file has been moved, Workbooks.Open gives run-time error 1004. To avoid it, check the file with the Dir function first:
Sub open_if_exists()
Dim myPath As String
myPath = "C:\Users\Dell\Desktop\myFile.xlsx"
If Dir(myPath) = "" Then
MsgBox "File not found: " & myPath
Else
Workbooks.Open Filename:=myPath
End If
End Sub
And if the workbook is already open, Workbooks.Open activates it (if it has unsaved changes, Excel first asks whether to reopen it and discard them). And if another file with the same name (from a different folder) is open, Excel shows an error, as two workbooks with the same name can’t be open at the same time.
Frequently Asked Questions
How do I open a workbook without updating links?
Use UpdateLinks:=0, like Workbooks.Open Filename:=myPath, UpdateLinks:=0. Excel then opens the file without updating the external references and without asking.
Can I open a workbook without showing it?
Turn off screen updating with Application.ScreenUpdating = False before you open it, and close it when you’re done. You can also set the window to hidden with ActiveWindow.Visible = False right after opening it, but then you need to unhide it before the user can see it.
How do I open a CSV file with VBA?
Use the same Workbooks.Open method with the path to the .csv file. If the data uses your local list separator (like a semicolon), add Local:=True so Excel splits the columns correctly.
Written by
Puneet Gogia
Microsoft MVP — Excel · Founder, Excel Champs
Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.