VBA Open Workbook (Excel File)

- Written by Puneet

To open a workbook using VBA, you need to use the “Workbook.Open” method and specify the path of the file (make sure to specify the full path to the workbook with name and extension file type). This method has a total of fifteen optional arguments which you can use 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 Workbook.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 Workbook.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.

Workbook.Open Syntax

Now it’s time to look at the syntax of the method that you just have used in the above example. 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

  1. Filename: Name of the file to be opened.
  2. 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.
  3. ReadOnly: True opens the workbook in read-only mode.
  4. Format: If Microsoft Excel opens a text file, this argument specifies the delimiter character
  5. Password: A String that’s the password required to open a protected workbook.
  6. WriteResPassword: A String that’s the password required to write to a write-reserved workbook.
  7. IgnoreReadOnlyRecommended: True to have Excel not display the read-only recommended message.
  8. Origin: If the file is a text file, this argument specifies the origin of the text file.
  9. 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.
  10. Editable: If the file is an Excel template, True to open the specified template for editing.
  11. Notify: If True, Excel doesn’t display the alert message if the workbook is not found. Instead, the error can be handled in the code through the Err object.
  12. 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. 
  13. AddToMru: True to add this workbook to the list of recently used files.
  14. Local: TRUE saves files against the language of Microsoft Excel (including control panel settings).
  15. 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 a workbook using vba
  1. Open Excel and press Alt + F11 to open the VBA Editor.
  2. In the VBA Editor, insert a new module by right-clicking on any of the objects in the Project window, then choose Insert > Module.
  3. In the module window, start the code, use the “Workbooks” object. Type a dot (.) after that and select the Open method from the list.
  4. 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.

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, in that case, 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"

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.

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()
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.
  • 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 Workbook Open File Dialog, (Once you learn to use it) you can write a dynamic codes.

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:=fmyPath)
    
' 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 both .xls and .xlsx file extensions.
  • A loop is then started with Do While myFile <> “”. This loop will continue as long as myFile is not an empty string, indicating 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 Files but Specific

This method is more suitable when you need to open multiple file 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 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.