Professional Documents
Culture Documents
Walter Milner
Sep-05 Slide:1
VBA in Excel
Introduction
• VBA = Visual Basic for Applications
• Enables end-user programming
• In MS Office applications
• Formulae and macros OK for simple actions, but..
• Advantages over formulae and macros:
– Multiple nested if is easy
– Loops easy
– Debugging easy
Sep-05 Slide:2
VBA in Excel
Getting started
View..Toolbars..Control
Sep-05 Slide:3
VBA in Excel
Add a button
Sep-05 Slide:4
VBA in Excel
Format it
Sep-05 Slide:5
VBA in Excel
Program it
Sep-05 Slide:6
VBA in Excel
Test it
Sep-05 Slide:7
VBA in Excel
Referring to cells
Sep-05 Slide:8
VBA in Excel
The VBA Excel Object model
• Hierarchy of classes
• Use to refer to things in
an Excel application
• Very large number of
classes, properties and
methods
• Have to use on-screen
help for reference
Sep-05 Slide:9
VBA in Excel
The VBA Excel Object model
• Object is a thing
• Property is some
property of an object
• Method is something
the object can be told
to do
• Event is something
that can happen to the
object
• Collection is a set of
related objects
• Enumeration is action
returning complete set
of things (eg all fonts
on system)
Sep-05 Slide:10
VBA in Excel
Example object - Application
Sep-05 Slide:11
VBA in Excel
Example properties of Application
Sep-05 Slide:12
VBA in Excel
Example method
Sep-05 Slide:13
VBA in Excel
Example collection -
Sep-05 Slide:14
VBA in Excel
Using For Each .. Next in a collection
Sep-05 Slide:15
VBA in Excel
The RangeSelection property of the ActiveWindow
Sep-05 Slide:16
VBA in Excel
2d array exercise
Sep-05 Slide:17
VBA in Excel