The Workbook object in VBA represents an Excel workbook. A workbook is the Excel file that contains worksheets, charts, modules, and other Excel content.
Using the Workbook object, you can open, close, save, activate, and work with Excel workbooks through VBA.
The Workbook object represents an Excel workbook in VBA.
For example, if you have an Excel file named Students.xlsx, that file is represented as a Workbook object in VBA.
Workbooks("Students.xlsx")
This refers to the workbook named Students.xlsx that is currently open.
The Workbook object allows VBA to control Excel files programmatically.
The Workbooks collection contains all currently open Excel workbooks.
Workbooks.Count
This returns the number of workbooks currently open in Excel.
MsgBox Workbooks.Count
You can refer to an open workbook by its file name.
Workbooks("Students.xlsx").Activate
This activates the workbook named Students.xlsx.
The workbook must already be open when using this reference.
The ActiveWorkbook object represents the workbook that is currently active.
MsgBox ActiveWorkbook.Name
This displays the name of the currently active workbook.
ThisWorkbook refers to the workbook in which the VBA code is stored.
MsgBox ThisWorkbook.Name
This displays the name of the workbook containing the running VBA code.
| Object | Meaning |
|---|---|
| ThisWorkbook | The workbook containing the VBA code. |
| ActiveWorkbook | The workbook currently active in Excel. |
These two objects can refer to different workbooks.
The Name property returns the workbook name.
MsgBox ThisWorkbook.Name
For example, if the workbook is named StudentResult.xlsm, the message box will display that name.
The Path property returns the folder location of the workbook.
MsgBox ThisWorkbook.Path
This can be useful when working with files stored in specific folders.
The FullName property returns the workbook's complete file name and path.
MsgBox ThisWorkbook.FullName
This can display something similar to:
C:\Students\StudentResult.xlsm
The Activate method makes a workbook the active workbook.
Workbooks("Students.xlsx").Activate
The specified workbook becomes active.
The Open method can open an existing Excel workbook.
Workbooks.Open "C:\Students\Students.xlsx"
VBA opens the workbook from the specified location.
The Close method closes a workbook.
Workbooks("Students.xlsx").Close
The specified workbook is closed.
Always be careful when closing workbooks because unsaved changes may need to be handled.
The Save method saves changes made to a workbook.
ThisWorkbook.Save
This saves the workbook containing the VBA code.
The SaveAs method saves a workbook with a new name or location.
ThisWorkbook.SaveAs "C:\Students\NewResult.xlsx"
Use SaveAs carefully because it can change the file name or file location.
The Add method can create a new workbook.
Workbooks.Add
This creates a new Excel workbook.
You can also store the new workbook in an object variable.
Dim wb As Workbook
Set wb = Workbooks.Add
You can declare a variable as a Workbook object.
Dim wb As Workbook
Set wb = ThisWorkbook
The variable wb now refers to the workbook containing the VBA code.
Once a workbook is stored in a variable, you can use that variable to access the workbook.
Dim wb As Workbook
Set wb = ThisWorkbook
MsgBox wb.Name
This displays the workbook name.
A Workbook object can be used to access its worksheets.
ThisWorkbook.Worksheets("Sheet1").Activate
This activates Sheet1 in the workbook containing the VBA code.
The Worksheets.Count property returns the number of worksheets.
MsgBox ThisWorkbook.Worksheets.Count
This displays the number of worksheets in the current workbook.
You can use a For Each loop to process all open workbooks.
Dim wb As Workbook
For Each wb In Workbooks
MsgBox wb.Name
Next wb
The code displays the name of every currently open workbook.
A Workbook object can be used with a For Each loop to process its worksheets.
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
MsgBox ws.Name
Next ws
This displays the name of each worksheet in the current workbook.
The Saved property can indicate whether a workbook has unsaved changes.
If ThisWorkbook.Saved = False Then
MsgBox "Workbook has unsaved changes"
End If
This can be useful before closing a workbook.
Workbook objects are useful in practical student projects.
For example, a student result project may contain:
VBA can use the Workbook object to manage these worksheets and automate operations.
The following example displays information about the workbook containing the VBA code.
Sub WorkbookInfo()
MsgBox "Name: " & ThisWorkbook.Name & vbCrLf & _
"Path: " & ThisWorkbook.Path
End Sub
This is a simple example of using Workbook properties.
Beginners commonly make these mistakes:
The following example creates a new workbook and writes a heading into its first worksheet.
Sub CreateReport()
Dim wb As Workbook
Set wb = Workbooks.Add
wb.Worksheets(1).Range("A1").Value = "Student Report"
wb.SaveAs "C:\Students\StudentReport.xlsx"
End Sub
This demonstrates how a Workbook object can be used to create and save a new report.
A typical Workbook object workflow is:
Dim wb As Workbook
Set wb = Workbooks.Add
wb.Worksheets(1).Range("A1").Value = "Hello Excel"
wb.SaveAs "C:\Students\Test.xlsx"
wb.Close
The Workbook object represents an Excel workbook and allows VBA to control Excel files programmatically.
Important Workbook objects and methods include:
Dim wb As Workbook
Set wb = ThisWorkbook
MsgBox wb.Name
MsgBox wb.Path
MsgBox wb.Worksheets.Count
The Workbook object is an important part of Excel VBA because most practical automation projects work with one or more Excel files.
The next lesson will introduce the Worksheet Object, which is used to work with individual worksheets inside a workbook.
Question: Which VBA object refers to the workbook that contains the running VBA code?