Vous êtes sur la page 1sur 20

Intro to VBA

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:

VBA specific topics:

Variable types and declarations,


Control logic,
Looping,
Arrays,
Subroutines and
Error handling

Excel object model,


recording macros,
working with ranges,
workbooks, worksheets, charts,
etc.,
developing user forms
(dialog boxes),
automating excel add-ins with
VBA code

1 of 20

Intro to VBA: Development in Excel


Developing Application with VBA requires

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

VB is not the same as VBA


VB:

VBA:

A software development
language that can be
bought and run separately
without the need for Excel

Comes with MS Office


Is the language you need
to manipulate Excel
Relatively easier

2 of 20

VBA The Remote Control for Excel 2007


VBA
development environment that is built into the MSO
suite of products
enables us to create custom functionality and
automate tasks in Office applications

VB, an object-oriented, event-driven language


works by accessing and manipulating objects
An object represents an element of an application like a
document, worksheet, chart, or dialog box

If VBA is the remote control for


Excel, then what is a macro?
&
What is the difference between
VBA and a macro?
6

3 of 20

Displaying the Developer Tab

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]

If referencing from R7C2

R1C1:R5C1 (absolute)
CV100 R100C100

R1C1
R[-2]C

A relative reference to the cell two


rows up and in the same column

R[2]C[2] A relative reference to the cell two


rows down and two columns to the
right
R2C2

An absolute reference to the cell in


the second row and in the second
column

R[-1]

A relative reference to the entire row


above the active cell

An absolute reference to the current row

10

5 of 20

The Excel Object Model


Objects ( nouns)
Things we see in the everyday world
Some excel objects have events that they respond to
e.g., worksheet object and Open event
Event Handler code runs when the associated event fires

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

Learning various objects, properties and methods is


learning vocabulary in English
11

Use Object Browser

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

Objects have properties


Author:
property of a workbook

Height:
property of a cell

Name:
property of a worksheet

Objects have methods


13

Collection of objects have methods

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

Close all workbooks


Workbooks.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

More than One:


Worksheets(Array(1,3,4)).select
15

Range
Point to a range with an address
Range("AA190").Select
Range("A12:B34").Select

Special Range Object Cells


Cells.Select
Cells.Item(5).Select
Cells.Item(4,2).Select

Special Range Object Colums / Rows


Columns(3).Select
Columns("D").Select
Rows("3:14").Select
16

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

CurrentRegion, EntireColums, EntireRow


Point to a region based on the current selection
ActiveCell.CurrentRegion.Select
ActiveCell.EntireColumn.Select
Selection.EntireRow.Select

18

9 of 20

The VBE (Alt + F11)

19

19

The VBE (Alt + F11)

Project Explorer
Properties Window
Code Window
Immediate, Local & Watch Windows
Debug, Edit, Standard & UserForm Toolbars

20

20

10 of 20

The VBE

21

Project Explorer: Lists all projects in any


open workbook
Each workbook has a project, and each project can
have several parts.
Objects: Workbook and worksheets
Modules: contain the VBA code for any macro
Standard modules
used for writing general procedures, or sections of code, to perform
different tasks with Excel objects

Class modules
used to create new properties and methods for more advanced coding.

Forms: the user interface tools in Excel

Use Insert-> Module or Insert > User Form to add a


new module or form to the current project; can also
use
icon from standard toolbar.
22

11 of 20

Project Explorer

23

Properties Window: has detailed information on a


selected part of a project in the Project Explorer

Basic naming and formatting


properties can be modified for
worksheets and workbooks
Important for user forms

24

Formatting
Position
Picture
Scrolling
Behavior
24

12 of 20

Properties Windows

25

Code Window: Displays the VBA code for the


highlighted part of a project in the Project Explorer

When macros are recorded they are


simultaneously created as VBA code in the
Visual Basic Editor
There are two types of procedures
Sub procedures
Function procedures

26

26

13 of 20

Immediate/ Watch Windows: for Debugging


Immediate ( Ctrl-g)
executes whatever is entered immediately
Useful for issuing one-line VBA commands
Preceding the command by a question (?) mark, results in an
immediate answer to the question

Can be used to (temporarily) print information


Using Debug.Print

Watch Window
displays values of inserted variables

Use View menu option to view or hide any window


27

Useful Toolbars

Standard

Edit

28

Debug
14 of 20

There are Four VBE Toolbars


Standard, Edit, Debug, and UserForm
The Standard toolbar allows to
Switch to the Excel window, insert a user form,
module, or procedure, and save our current code
modifications
Run, pause, or stop your code
View the design mode of our form design
Perform several standard editing options, such as
cut, copy, paste, search, undo, and redo

29

29

The Object Browser


Properties, Methods and Events
Libraries, Classes, and Members

30

30

15 of 20

Object Browser: provides a list of


properties, methods and events of all Excel
objects which may be manipulated in VBA
Properties:
the physical descriptions of Excel objects
The description of the particular property is called the value of the
property

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

Object Browser has larger categories


into which various objects are grouped
Libraries
= collections of VBA and Excel
object classes

Classes
= groups of related objects

Members
= properties, methods, and
constants of a selected class
of objects

View > Object Browser or


icon from standard toolbar
33

33

Object Browser (contd)


All classes containing
the searched object are
shown in the Search
Results window

All classes and


members of a selected
class are shown in the
second window
34

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

The first procedure encountered within the module


marks the end of the General Declarations section.
From that point on, the module will only contains
procedures.
18 of 20

37

38

19 of 20

Over to Session-1&2b.ppt

Examples

39

20 of 20

Vous aimerez peut-être aussi