Você está na página 1de 21

MS Excel

VBA Programming

Introducing VBA

Introducing VBA Programming


Visual Basic for Applications (VBA) a
programming language that helps program,
tweak, and squeeze productivity from Excel
Embedded in Excel
OOP - object-oriented programming

What Can You Do with VBA?

Inserting a text string


Automating a task you perform frequently
Automating repetitive operations (loops)
Creating a custom command
Creating a custom toolbar button
Creating a simplified front end
Developing new worksheet functions
Creating complete, macro-driven applications
Creating custom add-ins for Excel

Advantages and Disadvantages of VBA


Advantages

Disadvantages

Executes the task in exactly the same


way

Learning how to write programs in


VBA (language syntax, objects, props,)

Much faster than you could do it


manually

Other people who need to use your


VBA programs must have their own
copies of Excel

Always performs the task without


errors

Sometimes, things go wrong

Task can be performed by someone


who doesnt know anything about
Excel

VBA is a moving target

Can do things in Excel that are


otherwise impossible

VBA in a Nutshell (1)


You perform actions in VBA by writing (or
recording) code in a VBA module
A VBA module consists of Sub procedures
(macros and/or UDFs)
VBA manipulates objects
Objects are arranged in a hierarchy
Objects of the same type form a collection
You refer to an object by specifying its position
in the object hierarchy, using a dot as a
separator

VBA in a Nutshell (2)


If you omit specific references, Excel uses the
active objects
Objects have properties and methods
You refer to a property of an object by
combining the object name with the property
name, separated by a period
You can assign values to variables
VBA includes all the constructs of modern
programming languages, including arrays and
looping

Jumping Right In
Recording macro vs Using Excel built-in
function (for paste special, values only)
Live view macro recording sample

Visual Basic Editor (VBE)


Is connected, still
separate
application
Used to write, edit
and test code
Alt + F11

Working with the Project Explorer


Each Excel
workbook and
add-in thats open
is a project
Ctrl + R

Customizing the VBA Environment


Use the option in the Editor tab to control how
certain things work in the VBE

Excel Object Model

Excel Application Object


Addin object

CommandBar object
Window object
Workbook object

WorksheetFunction object

Workbook object
Chart object

Name object
VBProject object
Window object

Worksheet object

Worksheet object

Comment object
Hyperlink object
Name object
Outline object
PageSetup object
PivotTable object
Range object

Rangecontained in Worksheetcontained in Workbook contained in Excel

Collections of Objects
A collection is a group of objects of the same
type And a collection is itself an object !
Few examples of commonly used collections:
Workbooks collection
Worksheets collection
Charts collection
Sheets collection
collection names are all plural: Chart>Charts

Referring to Objects

(members of a collections)

Worksheets(Sheet1) by name
Worksheets(1) by order

Navigating through the hierarchy


Fully Qualified Reference:
Application.Workbooks(Book1.xls). _
Worksheets(1).Range(A1).Value
Assumed Reference:
Range(A1).Value

Object Properties and Methods


In order to manipulate objects:
Read or modify an objects properties
Specify a method of action to be used with an
object

Object properties
Every object has properties.
Properties are attributes that describe the object,
determine how it looks, how it behaves, and even
whether it is visible.
Using VBA, you can do two things with an
objects properties:
Examine the current setting for a property
Change the propertys setting

Object methods
A method is an action you perform with an
object
Most methods also take one or more arguments.
An argument is a value that further specifies the
action to perform.
Place the arguments for a method after the
method, separated by a space. Multiple
arguments are separated by a comma.

Object events
Objects respond to various events that occur
For example, when youre working in Excel and
you activate a different workbook, an Activate
event occurs

Você também pode gostar