Académique Documents
Professionnel Documents
Culture Documents
Session-1&2a
VBE
Object Browser
Important
Use object variables to refer to ranges,
workbooks, worksheets, etc.
Assured of getting Intellisense to list their
properties and methods
Need to learn
Gen. Programming topics:
1 of 20
A logical mind,
A willingness to experiment,
Using online help,
Plenty of practice, and
PERSEVERANCE
Enabling macros
Set macro security level to Medium
Check the Disable all macros with notifications
Use trust center settings for the folder containing
the files with VBA code
3
VBA:
A software development
language that can be
bought and run separately
without the need for Excel
2 of 20
3 of 20
R1C1
A1 notation
A1 notation
A: column
1: row
R1C1 notation
R1: row 1
C1: column 1
More possibilities:
R[-2]C same columns, 2 rows higher
R[1]C5 column 5, 1 row lower
4 of 20
A1
R1C1
E15 R15C5
A1:A5 R[-6]C[-1]:R[-2]C[-1]
R1C1:R5C1 (absolute)
CV100 R100C100
R1C1
R[-2]C
R[-1]
10
5 of 20
Properties ( adjectives)
Attributes of an object
Value
Each property has a value for any particular object
Methods ( verbs)
Things you can do to (or with) an object
Arguments ( Adverbs)
Qualifiers, indicating how a method is performed
Collections as Objects
Collection Object
An object that includes all of the
individual objects
Also have properties and
methods, but
they are not the same as the
properties and methods of the
objects they contain
Are fewer
Count property and Add method
Are plural
*There is no Ranges collection
object
A range object cannot be considered
a singular or plural
12
6 of 20
Cars (Car)
Wheels (Wheel)
Horn
Hood
HoodOrnament
Objects in Excel
Objects exists in collections
Workbook object
Workbook budget.xls instance of an object
Height:
property of a cell
Name:
property of a worksheet
Workbooks
Add a workbook to the collection
Workbooks.add
Name of a workbook
Workbooks.Item(1).name
Count workbooks
Workbooks.count
Close a workbook
Workbooks.Item(1).close
ActiveWorkBook.close
Activate Workbook
Workbooks(Book2).activate
Refer to a workbook
Workbooks.Item(1)
Workbooks(Book1)
14
7 of 20
Worksheets
Add a worksheet to the collection
Worksheets.add.name = June 2012
Delete a worksheet
Worksheets.Item(1).delete
Rename worksheet
Worksheets(1).name = Test
Activate worksheet
Worksheet(Test).activate
Copy worksheets
Worksheets(Test).Copy
Worksheets(Test).Copy Before:=Worksheet(1)
Select worksheets
One:
Worksheets(2).select
Range
Point to a range with an address
Range("AA190").Select
Range("A12:B34").Select
8 of 20
OffSet(m,n)
ActiveCell.offset(1,0).Select
Go one row down
ActiveCell.Offset(2,2).Select
Go two rows down
Go two columns to the right
ActiveCell.Offset(-1,-1).Select
Go one row up
Go one column to the left
17
18
9 of 20
19
19
Project Explorer
Properties Window
Code Window
Immediate, Local & Watch Windows
Debug, Edit, Standard & UserForm Toolbars
20
20
10 of 20
The VBE
21
Class modules
used to create new properties and methods for more advanced coding.
11 of 20
Project Explorer
23
24
Formatting
Position
Picture
Scrolling
Behavior
24
12 of 20
Properties Windows
25
26
26
13 of 20
Watch Window
displays values of inserted variables
Useful Toolbars
Standard
Edit
28
Debug
14 of 20
29
29
30
30
15 of 20
Methods:
actions that can be performed on an object
The elements of a method statement are called the arguments of the
method
Events:
actions that are performed on controls
A control is a user interface object, such as a button or check box
31
31
Object Browser
Property
Hand icon
Method
Green rectangular icon
Event
Lightning icon
32
16 of 20
Classes
= groups of related objects
Members
= properties, methods, and
constants of a selected class
of objects
33
34
17 of 20
Summary so far
The VBE is the environment in which one works with VBA
programming code
There are three main windows in VBE: the Code Window, the
Project Explorer, and the Properties Window
The Object Browser provides a list of properties and methods
of all Excel objects that may be manipulated in VBA
Properties are the physical descriptions of all Excel objects,
and the description of the particular property is called the value
of the property
Methods are the actions that can be performed on the object.
The elements of a method statement are called the arguments
of the method
Groups of related objects are called classes; collections of VBA
and Excel object classes are called libraries; and the
properties, methods, and constants of a selected class of
objects are known as the members of the class of objects.
35
35
Procedure
a general term that refers to a unit of code that
is created to perform a specific task.
Modules
a container for procedures
also provide an area in which compiler options
and various declarations may be stored.
Located at the very top of a module and is referred
to as the General Declarations section.
36
37
38
19 of 20
Over to Session-1&2b.ppt
Examples
39
20 of 20