How to Open a Workbook from a Path Using Excel VBA – 4 Examples

This is the sample dataset.

Excel VBA Open Workbook from Path


Example 1 – Using the VBA Workbooks Method to Open a Workbook from a Path in Excel

Steps:

  • Open your worksheet and save the Excel file as Excel Macro-Enabled Workbook (*xlsm).
  • Go to the Developer tab >> select Visual Basic.

Use of VBA Workbooks Method to Open Workbook from Path in Excel

  • In the Insert tab >> select Module.

  • Enter the Code below in Module1.
Sub Open_WB_from_Path()
Workbooks.Open "E:\Softeko\36\Open Workbook.xlsx"
End Sub

In the path location, mention the extension of the file.

Code Breakdown

  • The Open_WB_from_Path Sub Procedure is created .
  • The Workbooks Method is used to open the Workbook.
  • The Path location is provided between Inverted Commas.

  • Save the code.
  • Click Run.

You will see the workbook:

Read More: Excel VBA to Open Workbook in Background


Example 2 – Merging the Path and File Name Properties to Open a Workbook

Steps:

  • Open your worksheet and save the Excel file as Excel Macro-Enabled Workbook (*xlsm).
  • Go to the Developer tab >> select Visual Basic.

Merging Path and File Name Properties to Open Workbook Using Excel VBA

  • In the Insert tab >> select Module.

  • Enter the Code below in Module2.
Sub Open_my_WB()
 Dim my_P As String, my_F As String
 Dim my_WB As Workbook
    my_P = "E:\Softeko\36\"
    my_F = my_P & "Open Workbook.xlsx"
 Set my_WB = Workbooks.Open(my_F)
End Sub

Code Breakdown

  • The Open_my_WB Sub Procedure  is created.
  • The variables my_P and my_F are declared as String, and my_WB as Workbook.
  • my_P is the Path of the file.
  • my_F is the file name with location, which are concatenated with the ampersand operator (&) concatenates.
  • The Workbooks method is used to open the file.

  • Save the code and go back to the Excel File.
  • In the Developer tab >> select Macros.

  • Select Macro (Open_my_WB) and click Run.

You will see the workbook.

Read More: Excel VBA to Open Workbook from Path in Cell


Example 3 – Employing the Workbooks Method using a Cell Reference to Open a Workbook

Steps:

  • Enter the file location in C11.

Employing Workbooks Method with Cell Reference to Open Workbook in Excel

  • Follow the steps described in Example 1.
  • Enter the following Code in Module3.
Sub Open_WB_through_CellReference()
Dim WB_P As Variant
Set WB_P = Worksheets("Cell").Range("C11")
Workbooks.Open (WB_P)
End Sub

Using Cell Reference in VBA Code to Open Workbook in Excel

Code Breakdown

  • The Open_WB_through_CellReference Sub Procedure is created.
  • The WB_P variable is declared as Variant.
  • In WB_P the location is set through Cell Reference.
  • The Workbooks method is used to open the workbook.

  • Save the code and go back to the Excel sheet.
  • In the Developer tab >> select Macros.

  • Select Macro (Open_WB_through_CellReference) and click Run.

You will see the workbook.

Read More: How to Open Workbook and Run Macro Using VBA


Example 4 – Applying the GetOpenFilename Method to Open a Workbook from a Path in Excel

Steps:

  • Follow the steps described in Example 1.
  • Enter the following Code in Module4.
Sub Open_WB()
    Dim Dialog_Box_File As String
    Dialog_Box_File = Application.GetOpenFilename()
    Workbooks.Open (Dialog_Box_File)
End Sub

Applying GetOpenFilename Method to Open Workbook from Path in Excel VBA

Code Breakdown

  • The Open_WB Sub Procedure is created.
  • The Dialog_Box_File variable is declared as String.
  • The GetOpenFilename and Workbooks methods are used to open the workbook.

  • Save and Run the code. (Follow the steps described in Example 3.)

In the Open dialog box:

  • Select your file.
  • Click Open.

You will see the workbook.

Opened Workbook by Using Excel VBA

Read More: How to Open Workbook with Variable Name with Excel VBA


Download Practice Workbook

Download the practice workbook.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Musiha Mahfuza Mukta
Musiha Mahfuza Mukta

Musiha Mahfuza Mukta is a marine engineer who loves exploring Excel and VBA programming. To her, programming is a time-saving tool for dealing with data, files, and the internet. She's skilled in Rhino3D, Maxsurf, C++, MS Office, AutoCAD, and Excel & VBA, going beyond the basics. With a B.Sc in Naval Architecture & Marine Engineering from BUET, she's shifted gears and now works as a content developer. In this role, she creates techy content exclusively focused on Excel... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo