This is the sample dataset.
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.
- 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.
- 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.
- 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
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.
- 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
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.
Read More: How to Open Workbook with Variable Name with Excel VBA
Download Practice Workbook
Download the practice workbook.
Related Articles
- How to Open Workbook as Read-Only with Excel VBA
- How to Browse for File Path Using Excel VBA
- How to Open Folder and Select File Using Excel VBA
- How to Open File Dialog Default Folder with Excel VBA