Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

VBA Workbook Open

VBA-Workbook-Open

What is VBA Workbooks.Open?

As for as VBA is concerned I hope you have seen the magic and wonders VBA can do at your workplace. One of the substantial thing about VBA is it doesn’t limit its work in the same Workbook rather we can access the workbook which is already not opened. One of such a technique is we can open another workbook from a different workbook. In this special article, we will show you how to open excel workbook using VBA Workbooks.Open method in detail.

Open Another Workbook from Current Workbook

Just imagine a situation where you are working with two workbooks which are already opened. The workbook that you are working is named as “Book 1” and another workbook which is open but not active is named as “Book 2”.

Since we are working in “Book 1” in order to activate another workbook “Book 2” we need to specify the workbook name with its file extension by using WORKBOOKS object.

Excel VBA Workbook.Open Example 0

Inside this, we need to enter the workbook name and its extension.

Excel VBA Workbook.Open Example 0.1

Then we need to insist on what we need to do with this workbook. Since we need to activate this workbook select “Activate” method.

Excel VBA Workbook.Open Example 0.2

So, this code will activate the workbook “Book 2.xlsx” if it is opened.

What if the workbook “Book 2.xlsx” is not opened??? How do you open it or activate it????

This is where our Excel VBA tool Workbooks.Open method comes into the picture.

VBA Workbooks.Open Method Syntax

Below is the syntax of Workbooks.Open method.

Excel VBA workbooks.open method

File Name: The first argument in this method is we need to specify the workbook name that we are trying to open. Workbook name alone cannot do the job here, because excel doesn’t know in which folder your workbook is saved. So we need to supply full folder path followed by an exact file name with its saved extension.

Update Links: When we try to open the workbook it may have contained some external links from other workbooks. We can supply yes or no here.

Read Mode: How do you want to open the workbook?? If you want the file only to be read then TRUE is the argument.

Password: If the targeted or opening workbook contains any password then in this argument we need to specify the password that was used while protecting the workbook.

These four arguments are good enough in this method. Do not break your head to understand each and every argument because the need for those remaining arguments many never arises.

Examples of VBA Workbook.Open Method

Below are the examples of VBA Workbooks.Open method.

You can download this VBA Workbook Open Excel Template here – VBA Workbook Open Excel Template

VBA Workbook Open – Example #1

Imagine you are trying to open the file named “File 1.xlsx” on your computer. File is saved in so many folders and subfolders. In order to open this workbook through VBA coding follow below steps.

Step 1: Start the subprocedure.

Code:

Sub Workbook_Example1()

End Sub

Excel VBA Workbook.Open Example 1.1

Step 2: Inside the VBA subprocedure start Workbooks.Open method.

Excel VBA Workbook.Open Example 1.2

Step 3: The very first thing we need to mention is file name with its folder path and extension of the file.

For this first, we need to open the exact file location.

Excel VBA Workbook.Open Example 1.3

Now what you have to do is you need to copy the file path by using folder path.

Workbook.Open Example 1.4

Step 4: Now copy the link and paste in coding as follows.

Code:

Sub Workbook_Example1()

Workbooks.Open Filename:="D:\Excel Files\VBA\File1.xlsx"

End SubM

Workbook.Open Example 1.5

So, now we have copied and pasted the file path where it is stored. In this mentioned file path it may contain many files, so after this enter backward slash first and then enter the file name with a file extension.

Code:

Sub Workbook_Example1()

Workbooks.Open Filename:="D:\Excel Files\VBA\File1.xlsx"

End Sub

Workbook.Open Example 1.6

Now ignore all the other arguments.

Step 5: Run the code it will open up the Excel workbook named as “File 1.xlsx”.

So, our code has just opened the mentioned workbook in the mentioned folder path.

VBA Workbook Open – Example #2

The best and efficient way of using VBA coding boils down to use of variables while coding. Because as part of large VBA project we may need to open other files in between the coding, so in such situations use of variables going to play an important role.

The same file can be opened by using variables as well. For this declare two variables as a string data type.

Code:

Sub Workbook_Example2()

    Dim File_Location As String
    Dim File_Name As String

End Sub

Example 2.1

For File_Location variable assign the folder path where the file is stored.

Code:

Sub Workbook_Example2()

    Dim File_Location As String
    Dim File_Name As String

    File_Location = "D:\Excel Files\VBA\"

End Sub

Example 2.2

Note: One extra thing we need to do is after pasting the link we need to put backward slash.

Now for File_Name variable, we need to mention file name with its excel extension.

Code:

Sub Workbook_Example2()

    Dim File_Location As String
    Dim File_Name As String

    File_Location = "D:\Excel Files\VBA\"
    File_Name = "File1.xlsx"

End Sub

Example 2.3

Now, these two variables combine to create a full folder path.

Now open Workbooks.Open method again in excel VBA.

Example 2.4

For the first argument supply, two variable names with an ampersand (&) sign.

Code:

Sub Workbook_Example2()

    Dim File_Location As String
    Dim File_Name As String

    File_Location = "D:\Excel Files\VBA\"
    File_Name = "File1.xlsx"

    Workbooks.Open File_Location & File_Name

End Sub

Example 2.6

So now we can change the folder path and file name whenever we want to for just variables, so wherever we use variables it will reflect the changes instantly.

Things to Remember

  • Never ever enter the file location manually because 99% of the time we make mistakes while typing the location, so copy from the location only.
  • To separate location and file we need to enter a backward slash (\) manually.
  • If the file is protected by a password then use PASSWORD argument under VBA Workbooks.Open method.

Recommended Articles

This has been a guide to VBA WorkBook Open Method. Here we discuss how to open an excel workbook from another workbook using VBA Workbooks.Open method. You can learn more from the following VBA articles –

  • VBA Workbook Object
  • VBA ThisWorkbook Property
  • VBA Worksheets Object
  • Excel VBA Worksheet Function

The post VBA Workbook Open appeared first on .



This post first appeared on Free Investment Banking Tutorials |WallStreetMojo, please read the originial post: here

Share the post

VBA Workbook Open

×

Subscribe to Free Investment Banking Tutorials |wallstreetmojo

Get updates delivered right to your inbox!

Thank you for your subscription

×