Excel VBA Lesson 21: The Workbook Object

<<Lesson 20>> [Contents] <<Lesson 22>>

In the previous lesson, we have learned to write code associated with the worksheet object. In this lesson, we shall learn about the Workbook object. The Workbook object at the top of the hierarchy of the Excel VBA objects. We will deal with properties and methods associated the Workbook object.

21.1 The Workbook Properties

When we write Excel 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 21.1

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

Excel VBA

Running the program produces a message box that displays the first workbook name, i.e. workbook_object1.xls as shown in Figure 17.1 below:

Figure 21.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 21.2

Private Sub CommandButton1_Click ()
MsgBox ThisWorkbook.Path
End SubOr you can use the following code:

Private Sub CommandButton1Click ()
MsgBox Workbooks (“workbook_object1.xls”).Path
End Sub

The output is shown below:

Excel VBA

Figure 21.2

Example 21.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

Or

Private Sub CommandButton1Click()
MsgBox Workbooks(“workbook_object1.xls”).Fullname
End Sub

The output is shown in Figure 17.3.

Excel VBA

Figure 21.3

21.2 The Workbook Methods

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

Example 21.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 17.4.

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

Excel VBA

Figure 21.4

Another method associated with the workbook object is open. The syntax is
Workbooks.Open (“File Name”)

Example 21.5

In this example, when the user clicks on the command button, it will open the file workbook_object1.xls under the path C:\Users\liewvk\Documents\
Private Sub CommandButton1_Click()
Workbooks.Open (“C:\Users\liewvk\Documents\workbook_object1.xls”)
End Sub

The close method is the command that closes a workbook. The syntax is
Workbooks (i).Close

Example 21.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



<<Lesson 20>> [Contents] <<Lesson 22>>