0% found this document useful (0 votes)
20 views15 pages

Lecture 15

The document discusses Excel's object model and VBA. It explains that Excel's object model is hierarchical, with the Application object at the top. It then discusses collections, object referencing, and different objects like the Range and Cells objects. The Range object represents a cell or range of cells, and can be referenced using the Range, Cells, or Offset properties of worksheet or range objects.

Uploaded by

Abby
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)
20 views15 pages

Lecture 15

The document discusses Excel's object model and VBA. It explains that Excel's object model is hierarchical, with the Application object at the top. It then discusses collections, object referencing, and different objects like the Range and Cells objects. The Range object represents a cell or range of cells, and can be referenced using the Range, Cells, or Offset properties of worksheet or range objects.

Uploaded by

Abby
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/ 15

2/17/2012

Lecture 15: Excel VBA

Introduction to Computer
Programming for Engineering

Lecture 15: Excel VBA

EXCEL OBJECT MODEL

1
2/17/2012

Excel Object Model

 Similar to a Family tree or Pedigree Chart

Excel Object Model

2
2/17/2012

Excel Object Model

 Shows the hierarchy and


relationship for all the objects,
properties, methods, and events contained
in Excel.

Excel Object Model

 At the top of this model is the


Application object — in this case,
Excel itself.

3
2/17/2012

Application Object
• a collection of all
Workbooks
Workbook objects

• a collection of all
Windows
Window objects

• a collection of all
AddIns
AddIn objects

Workbook Object
• a collection of
Worksheets
Worksheet objects

• a collection of Chart
Charts
objects

• a collection of Name
Names
objects

4
2/17/2012

Lecture 15: Excel VBA

COLLECTIONS

Collections

 A collection is a group of objects


of the same class, and

 a collection is itself an
object.

5
2/17/2012

Collections

 You can work with an entire collection of


objects or with an individual object in a
collection.

Collections

 To reference a single object from a


collection, you put the object’s name or
index number in parentheses after the
name of the collection

Worksheets(“Sheet1”)

6
2/17/2012

Lecture 15: Excel VBA

OBJECT REFERENCING

Object Referencing

 When you refer to an object using VBA,


you often must qualify the object by
connecting object names with a period
(also known as a dot operator).

7
2/17/2012

Object Referencing

 Cell A1 (A-One) of Active Sheet

Range(“A1”)

Object Referencing

 Cell A1 of Sheet1 Worksheet

Worksheets(“Sheet1”).Range(“A1”)

8
2/17/2012

Object Referencing

 Cell A1 of Sheet1 Worksheet in Book1


Workbook

Workbooks(“Book1”).Worksheets(“Sheet1
”).Range(“A1”)

Object Referencing

 The fully qualified reference of Cell A1


including the Application Object.

Application.Workbooks(“Book1”).Workshe
ets(“Sheet1”).Range(“A1”)

9
2/17/2012

Lecture 15: Excel VBA

EXCEL APPLICATION
OBJECT

Excel Application Object

 Represents the entire Microsoft Excel


application.

10
2/17/2012

Excel Application Object

Application-wide settings and options.

Properties that return top-level objects,


such as ActiveCell, ActiveSheet, and so on.

Excel Application Model


Property Object Returned
ActiveCell The active cell.
ActiveChart The active chart sheet or chart
contained in a ChartObject on a
worksheet.
This property is Nothing if a chart
isn’t active.
ActiveSheet The active sheet (worksheet or
chart).
ActiveWindow The active window.

11
2/17/2012

Excel Application Model


Method Object Returned
ActiveWorkbook The active workbook.
Selection The object selected. (It could be a
Range object, Shape, ChartObject, and
so
on.)
ThisWorkbook The workbook that contains the VBA
procedure being executed.This object
may
or may not be the same as the
ActiveWorkbook object.

Lecture 15: Excel VBA

CELL RANGES

12
2/17/2012

Range Object

 A Range object is contained in a


Worksheet object and consists of a
single cell or range of cells
on a single worksheet.

Range Object Referencing

 Ways of referring to Range objects in


your VBA code:
1. The Range property of a Worksheet or
Range class object
2. The Cells property of a Worksheet object
3. The Offset property of a Range object

13
2/17/2012

Range Property

 The Range property returns a Range


object.

 Syntax:
◦ object.Range(cell1)
◦ object.Range(cell1, cell2)
◦ object.Range(cell1:cell2)
◦ object.Range(NamedRange)

Cells Property
 Another way to reference a range is to
use the Cells property. You can use the
Cells property, like the Range property, on
Worksheet objects and Range objects.

 Syntaxes:
◦ object.Cells(rowIndex, columnIndex)
◦ object.Cells(rowIndex)
◦ object.Cells

14
2/17/2012

Offset Property

 The Offset property, like the Range


and Cells properties, also returns a
Range object.

 But unlike the other two methods, the


Offset property applies only to a
Range object and no other class.

Offset Property

 Syntax:
◦ object.Offset(rowOffset, columnOffset)

15

You might also like