Anda di halaman 1dari 28

Visual Basic for Applications

(VBA)

Sessions 10-12: Demonstrations


using VBA
Clarifications on Assignment 2
Macros as Viruses and Recorded Macros
Changes needed to implement Repetitive-Regression
Challenge: Iconset Conditional Formatting via modified
macro
Later:
Function to calculate weighted average of a series with 2
inputs
Named Ranges in Excel, and their access inside VBA.
Running macros from command buttons on Excel
spreadsheet
Message Boxes in Excel
2

But First: Just what is a Macro ?


A Macro is programmatic control over
Visual activities of XL
Macros may well be viruses
Macros could be our introduction to
Software building
VBA is Microsofts programming
environment that is portable
However, Quirks abound
Also remember that while WYSIWYG,
WYSINATE
3

Objects: In VBA tasks are performed by


manipulating objects

Properties

A property is an attribute of an object that defines


one of its characteristics, such as its name, size,
color, or location on the screen, etc.

Examples

Range("A1:B15").Rows.Count (a read-only property)


Range("A1:B15").Font.Bold = True (read-write property)

Methods

Methods are the actions that can be performed on


Objects, such as closing a workbook

Methods for Objects

Range("A1:B15").ClearContents

ActiveCell.Copy

Events

Objects can respond to Events, such as a Mouse-Click,


Double Click on a cell, Activate a Worksheet,
Open/Close/Save a Workbook, etc.
Worksheet events:
Such as Activate, Deactivate, Change, Selection
Change, Before Double Click, etc.
Workbook Events:
Such as Open, Before Close, Before Saving, Before
Print, New Sheet.

Collection Objects
Objects are often grouped into collections, which
are themselves objects, called collection
objects
Collection Objects share Common Properties, Methods and Events
Examples : Workbooks, Worksheets, etc.
Worksheets is a collection of all the Worksheet objects in the specified or
active workbook.

Referring Objects
Worksheets(1) refers to the 1st worksheet of current active workbook.
Worksheets(Annual) refers to the worksheet named Annual.

Referring to Objects
VBA organizes objects and object collections in a hierarchy
with the Excel application at the top and the individual cells
of a workbook at the bottom

10

Referring to Property of Objects

11

Applying Methods

12

Working with Variables


A variable is a named element in a program that
can be used to store and retrieve data
Every variable is identified by a unique variable
name
Dim variable as type

13

Variables
Better to use Data Types:
Dim amount As Double
Dim name As String
Other data types: Boolean, Byte, Currency, Date, etc
Default (no type) is Variant
Option Explicit forces all variables to be declared
Can modify scope (outside Subs & Functions)
Private L As Integer
(only current module)
Public billsPaid As Currency
(available to any module)

14

User interaction : InputBox

15

Message Box
MsgBox Prompt, Buttons, Title

16

User Form
These are custom made dialogue boxes
Appropriate Controls from the Toolbox Controls can be added as required
Properties of a control can be changed
VBA Sub procedures to handle events (Event handlers)
generated in the User form need to be written by the programmer

17

Writing to a Sheet
Put the absolute value of the variable FG in row 2 ,
column T of the Worksheet named mySheet.

Worksheets(mySheet).Range(T2)=VBA.Abs(FG)

18

Reading from a Worksheet


To read in a value, use the .Value method, applying the
same ideas used for writing:

X = Worksheets(mySheet).Range(T2).Value

19

Ways to use .Range Method


Range("A1")

Cell A1

Range("A1:B5")

Cells A1 through B5

Range("C5:D9,G9:H16")

A multiple-area selection

Range("A:A")

Column A

Range("1:1")

Row 1

Range("A:C")

Columns A through C

Range("1:5")

Rows 1 through 5

Range("1:1,3:3,8:8")

Rows 1, 3, and 8

Range("A:A,C:C,F:F")

Columns A, C, and F
20

Use an Excel Function


VBA has a limited number of built-in functions, but you
can access the plethora of Excel worksheet functions.
This example uses the Excel ROUNDDOWN function

W = WorksheetFunction.Rounddown(Value)

21

Control Structures
Decisions
If anyDate < Now Then
anyDate = Now
End If
Next, consider If Then Else

22

Decisions(contd.)
If Index = 0 Then
X=X+1
Y = VBA.Sqr(X)
Else If Index = 1 Then
Y = VBA.Sqr(X)
Else If Index = 2 Then
Y=X
Else
X=0
End If

23

Decisions(contd.)
Select Case IndexVariable
Case 0
statements
Case 1 to 10
statements
Case Is < 0
statements
Case NumSteps
statements
Case Else
statements
End Select

Notice that the cases can


be constants, range
values, conditions and
variables; this is a
powerful control structure
that we will use to select
events to execute

24

Loops/Iterations

Do {While|Until} condition
statements
Loop

lCount = 0
lNum = 10
Do Until lNum = 0
lNum = lNum 1
lCount = lCount + 1
Loop
lCount = 0
lNum = 10
Do While lNum > 0
lNum = lNum 1
lCount = lCount + 1
Loop

25

Loops(contd.)
For counter = start To end [Step increment]
statements
Next counter
Total =0
For COUNTER=1 To 10
Total = Total + COUNTER
Next COUNTER

26

Exit Commands
Exit Function:
Immediately exits the Function procedure in which it appears.
Execution continues with the statement following the statement that called
the Function.

Exit Sub:
Immediately exits the Sub procedure in which it appears.
Execution continues with the statement following the statement that called
the Sub procedure.

27

Thank YOU

Anda mungkin juga menyukai