[go: up one dir, main page]

0% found this document useful (0 votes)
49 views2 pages

Chapter 3.1 - Workbook and Worksheet Object

The document discusses the workbook and worksheet objects in Excel VBA. It explains the object hierarchy with Excel as the application object containing workbook objects which contain worksheet objects. It describes how workbooks and worksheets are collections that can be referenced individually and provides examples of using properties like Count and methods like Add.

Uploaded by

putra.stat11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views2 pages

Chapter 3.1 - Workbook and Worksheet Object

The document discusses the workbook and worksheet objects in Excel VBA. It explains the object hierarchy with Excel as the application object containing workbook objects which contain worksheet objects. It describes how workbooks and worksheets are collections that can be referenced individually and provides examples of using properties like Count and methods like Add.

Uploaded by

putra.stat11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Excel Easy

#1 Excel tutorial on the net Excel Introduction Basics Functions Data Analysis VBA 300 Examples Ask us

Workbook and Worksheet Object Search Entire Site Go

Object Hierarchy | Collections | Properties and Methods


Chapter < >
Learn more about the Workbook and Worksheet object in Excel VBA.

Workbook and Worksheet Object


Object Hierarchy
In Excel VBA, an object can contain another object, and that object can contain another object, etc. In other
words, Excel VBA programming involves working with an object hierarchy. This probably sounds quite
confusing, but we will make it clear. Learn more, it's easy
The mother of all objects is Excel itself. We call it the Application object. The application object contains other Path and FullName
objects. For example, the Workbook object (Excel file). This can be any workbook you have created. The
Workbook object contains other objects, such as the Worksheet object. The Worksheet object contains other Close and Open

objects, such as the Range object.


Loop through Books and Sheets

Sales Calculator
The Create a Macro chapter illustrates how to run code by clicking on a command button. We used the
following code line:
Files in a Directory
Range("A1").Value = "Hello"
Import Sheets
but what we really meant was:
Programming Charts
Application.Workbooks("create­a­macro").Worksheets(1).Range("A1").Value = "Hello"

Note: the objects are connected with a dot. Fortunately, we do not have to add a code line this way. That is
because we placed our command button in create­a­macro.xls, on the first worksheet. Be aware that if you want
Download Excel File
to change things on different worksheets, you have to include the Worksheet object. Read on.
workbook­worksheet­object.xls
Collections
You may have noticed that Workbooks and Worksheets are both plural. That is because they are collections.
The Workbooks collection contains all the Workbook objects that are currently open. The Worksheets collection Follow Excel Easy
contains all the Worksheet objects in a workbook.

You can refer to a member of the collection, for example, a single Worksheet object, in three ways.

1. Using the worksheet name.

Worksheets("Sales").Range("A1").Value = "Hello"

2. Using the index number (1 is the first worksheet starting from the left).

Worksheets(1).Range("A1").Value = "Hello"

3. Using the CodeName.

Sheet1.Range("A1").Value = "Hello"

To see the CodeName of a worksheet, open the Visual Basic Editor. In the Project Explorer, the first name is the
CodeName. The second name is the worksheet name (Sales).
Note: the CodeName remains the same if you change the worksheet name or the order of your worksheets so
this is the safest way to reference a worksheet. Click View, Properties Window to change the CodeName of a
worksheet. There is one disadvantage, you cannot use the CodeName if you reference a worksheet in a
different workbook.

Properties and Methods


Now let's take a look at some properties and methods of the Workbooks and Worksheets collection.
Properties are something which an collection has (they describe the collection), while methods do something
(they perform an action with an collection).

Place a command button on your worksheet and add the code lines:

1. The Add method of the Workbooks collection creates a new workbook.

Workbooks.Add

Note: the Add method of the Worksheets collection creates a new worksheet.

2. The Count property of the Worksheets collection counts the number of worksheets in a workbook.

MsgBox Worksheets.Count

Result when you click the command button on the sheet:

Note: the Count property of the Workbooks collection counts the number of active workbooks.

1/8 Completed! Learn more about books and sheets >


Back to Top: Workbook and Worksheet Object | Go to Next Chapter: Range Object

EXCEL EASY • FREE EXCEL HELP • COPYRIGHT (C) 2010­2017 • ALL RIGHTS RESERVED
MICROSOFT EXCEL TUTORIAL | HOW TO USE VBA | EXCEL FORMULAS | DROP­DOWN LIST

You might also like