Excecl VBA Classic Excel VBA 2010 Excel VBA 365 Excel VBA Examples About Us

Lesson 20 The Workbook Object

The Workbook object lies at the top of the hierarchy of the Excel 2010 VBA objects. We will deal with properties and methods associated the Workbook object.

20.1 The Workbook Properties

When we write Excel 2010 VBA code involving the Workbook object, we use Workbooks.

The reason is that we are dealing with a collection of workbooks most of the time, so using Workbooks enables us to manipulate multiple workbooks at the same time.When will deal with multiple workbooks, we can use indices to denote different workbooks that are open, using the syntax Workbooks (i), where i is an index. For example, Workbooks (1) denotes Workbook1, Workbooks (2) denotes Workbook2 and more.

Workbooks have a number of properties. Some of the common properties are Name, Path and FullName Let’s look at the following example:

Example 20.1

Private Sub CommandButton1_Click()
 MsgBox Workbooks(1).Name
End Sub

The program will cause a message dialog box to pop up and displays the first workbook name, i.e. Book1 as shown in Figure 20.1 below:

Figure 20.1

If we have only one open workbook, we can also use the syntax ThisWorkbook in place of Workbook (1), as follows:

Private Sub CommandButton1_Click ()
 MsgBox ThisWorkbook.Name
End Sub

Example 20.2

Private Sub CommandButton1_Click ()
 MsgBox ThisWorkbook.Path
End Sub

Or you can use the following code:

Private Sub CommandButton1Click ()
 MsgBox Workbooks (“Book1”).Path
End Sub
Figure 20.2

Example 20.3

This example will display the path and name of the opened workbook. The code is:

Private Sub CommandButton1_Click ()
 MsgBox ThisWorkbook.FullName
End Sub


Private Sub CommandButton1Click()
 MsgBox Workbooks(“Book1”).Fullname
End Sub
Figure 20.3

20.2 The Workbook Methods

There are severl methods associated with the workbook object. These methods are Save, SaveAs, Open, Close and more.

Example 20.4

In this example, when the user clicks on the command button, it will open up a dialog box and ask the user to specify a path and type in the file name, and then click the save button, not unlike the standard windows SaveAs dialog, as shown in Figure 20.4.

Private Sub CommandButton1_Click()
 fName = Application.GetSaveAsFilename
 ThisWorkbook.SaveAs Filename:=fName
End Sub
Figure 20.4

Another method associated with the workbook object is open. The syntax is

Workbooks.Open (“File Name”)

Example 20.5

In this example, when the user click on the command button, it wil open the file book1.xls under the path C:\Users\Toshiba\Documents\Liew Folder\VBA\vba2010\

Private Sub CommandButton1_Click()
 Workbooks.Open ("C:\Users\Toshiba\Documents\Liew Folder\VBA\vba2010\book1.xlsx")
End Sub

The close method is the command that closes a workbook. The syntax is

Workbooks (i).Close

Example 20.6

In this example, when the user clicks the command button, it will close Workbooks (1).

Private Sub CommandButton1_Click()
 Workbooks (1).Close
End Sub

Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved   [Privacy Policy]

Contact: Facebook Page