Object Oriented Programming
Object Oriented Programming
Visual Basic
Objects / Properties / Methods
Object
Property
Method
Noun
Adjective
Verb
Visual Basic
Range (A3).select
Range is an object
(A3) is a modifier for the object
select is a method
Note that object separated from method by a .
Visual Basic
Activesheet.name
Activesheet is an object
name is a property
The result is the name of the active sheet
Procedures
Sub procedures return no values
Function Procedures
Function Grade (exam1,exam2,exam3) as String
Sum = exam1+exam2+exam3
If sum > 95 then
Grade = A
Else if
sum > 80
Grade = B
Else
Grade = C
End if
End Function
The function name must be assigned
the value to be returned!
Sub Procedures
Sub Gc()
Lines beginning with are comments
' Gc Macro
' Puts gc in active cell & units in adjacent cell to right
' Keyboard Shortcut: Ctrl+g
'
ActiveCell.FormulaR1C1 = "32.174"
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "ft-lbm/lbf-s^2"
End Sub
Visual Basic
Sub NameIt()
Dim newname as String
newname = InputBox(Enter a name for the worksheet)
ActiveSheet.Name=newname
End Sub
Option Explicit
Forces you to Dim all variables
Helps prevent typos
Can set this as the default through the
Tools/ Options menu.
Require variable declaration check box)
Visual Basic
All objects have properties
Most objects have methods
Will work with only a few of the many
objects, methods and properties
To get a list of objects and properties,
invoke the Object Browser
Visual Basic
Some of the objects
Workbook
Worksheet
ActiveSheet
Range
Form
Chart
Note --- a Cell is not an object, but is a type of
range object
Visual Basic
And to further confuse the issue
objects can have objects as
properties
And objects can be grouped into
collections
Workbook = collection of
worksheets
Visual Basic
Object Browser --- Just for a quick look
In VBA, press F2 or View / Browser
Select Excel from library list
Enter an object in the second list box
Use F1 to get help on an object
A=5
If ( A > 5) then .
A = sqrt (12)
Iteration Statements
For I = 1 to 6
Read 5, A
Next i
-4
9
-8
11
True
False
False
Conditional Expressions
If / then / else end if
Select case . End Case
If (expression) then
One or more expressions
else
One or more expressions
end if
Simple form
If (expression ) then (expression)
Iteration Statements
Used to execute a number of
statements repeatedly
Two major types --For / Next
Do / Loop
Do while expression_is_true
Statements
Loop
Do
Statements
Loop while expression_is_true
Do Until expression_is_true
Statements
Loop
Do
Statements
Loop Until expression_is_true
Sine =
i 1
(1)
( i 1
angle
2i 1!
( 2 i 1)
Function will take two argument the angle and n, then number
Of terms in the series. It will return the approximate value of
)
the sine. (In theory, n should be