What is the VBA code for opening a workbook from a path?


You can use the Workbooks.Open method in VBA to open an Excel workbook from a specific path.

Here is one common way to use this function in practice:

Sub OpenWorkbook()

Dim wb As Workbook
Dim FilePath As String

FilePath = InputBox("Please Enter File Path")
Workbooks.Open FilePath

End Sub

When this macro is run, an input box will appear where a user can type in the path to a specific Excel workbook.

The macro will then automatically find the workbook located in the specific path and open it in Excel.

The following example shows how to use this syntax in practice.

Example: How to Open Workbook from Path Using VBA

Suppose we have an Excel workbook called my_workbook2.xlsx located in the following location:

C:UsersbobDocumentsmy_workbook2.xlsx

Suppose we would like to use VBA to automatically open this workbook.

We can create the following macro to do so:

Sub OpenWorkbook()

Dim wb As Workbook
Dim FilePath As String

FilePath = InputBox("Please Enter File Path")
Workbooks.Open FilePath

End Sub

Once we run this macro, a box will appear where we can type in the path to the workbook in the input box:

Once we click OK, the macro will open the Excel workbook located in the path that we specified.

Note that if you enter the name of a workbook in a file path that does not exist, VBA will throw an error message.

For example, suppose we attempt to open a workbook named my_workbook3.xlsx, which does not exist.

This lets us know that the file path we specified does not exist.

Note: You can find the complete documentation for the Workbooks.Open method in VBA .

x