Business Computing
Business Computing
(VBA)
Properties
Examples
Methods
Range("A1:B15").ClearContents
ActiveCell.Copy
Events
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
11
Applying Methods
12
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
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
X = Worksheets(mySheet).Range(T2).Value
19
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
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
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