visual basic objects / properties / methods propertyadjective objectnoun part of the application...
DESCRIPTION
Activesheet.name Activesheet is an object name is a property Visual Basic The result is the name of the active sheetTRANSCRIPT
![Page 1: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/1.jpg)
Visual Basic
Objects / Properties / Methods
Property Adjective
Object Noun Part of the application
Attribute
Method Verb Action to dosomething
![Page 2: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/2.jpg)
Visual Basic
Range (“A3”).select
Range is an object
select is a method
(“A3”) is a modifier for the object
Note that object separated from method by a .
![Page 3: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/3.jpg)
Activesheet.name
Activesheet is an object
name is a property
Visual Basic
The result is the name of the active sheet
![Page 4: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/4.jpg)
Procedures
Sub procedures return no values except by changes to parameters in the argument list
Function procedures return a value by assigning itto the name of the function.
![Page 5: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/5.jpg)
Function Procedures
Function Grade (exam1,exam2,exam3) as String
Sum = exam1+exam2+exam3If Sum > 95 then Grade = “A”Elseif Sum > 80 Grade = “B”Else Grade = “C”Endif End Function
Dim Sum as Single
![Page 6: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/6.jpg)
For functions and sub procedures, there is a one to one correspondence between the arguments in the call to the function and the argument list of the function:
Function Grade (exam1,exam2,exam3) as String
The call:= Grade( 90, C40, $C$60 )
The definition:
![Page 7: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/7.jpg)
Sub Procedures
Public Sub ColorList()' Lists all the available colors in the current pallette' Number is listed in column 2, color sample in column 3' List starts in row 5Dim I As IntegerDim J As IntegerDim K As IntegerJ = 5I = 2For K = 1 To 56 Cells(J, I).Value = K Cells(J, I + 1).Interior.ColorIndex = K J = J + 1Next KEnd Sub
Note that ColorList is never assigned a value!
![Page 8: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/8.jpg)
Review of some Excel Basics
Cell references of two types:
A1 Columns are alphabetic, rows are numeric
R1C1 R number Column number
B2 and R2C2 refer to the same cell
Can be set by the Tools / Options menus
(Note that the two methods are transposed –A1 – column first, then row R1C1 – row first, then column )
![Page 9: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/9.jpg)
Formulae in Excel (R1C1 style)
R1C1 is an absolute address – it does not change under copy / paste
R[1]C[1] is a relative address – it does change under copy /paste
And can use mixed mode:
R1C[1] – R1 is absolute, C[1] is relative
![Page 10: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/10.jpg)
In VBA, can use either or both A1 and R1C1 styles
A1 style tends to be absolute
A1 style used with the “Range” objectRange(“A4”)
Can refer to individual cells with the “Cells” object, which uses an R1C1 styleCells(4,1)
![Page 11: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/11.jpg)
In Excel, cells & ranges can be namedInsert / name menu
These names can be used in FormulaeVBA
![Page 12: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/12.jpg)
VBA Variable Types
String A sequence of bytes containing charactersInteger 2 byte integerLong 4 byte integerSingle 4 byte real numberDouble 8 byte real numberVariant Can hold anything (but “expensive” to use)“Object” A class of data types used by Excel/VBA
![Page 13: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/13.jpg)
Characters & Stings
For Excel, anything that is not a number or a formula is a string. If want a set of digits as a string, need to enclose in quote or quotation marks.
For VBA, need to define variable that will hold strings as string data types
![Page 14: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/14.jpg)
Visual Basic
Sub NameIt()Dim newname as Stringnewname = InputBox(“Enter a name for the worksheet”)ActiveSheet.Name=newnameEnd Sub
Example -- simple Sub to name the active worksheet:
![Page 15: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/15.jpg)
“Object” data type
Many of the objects in Excel have their owndata type:
Dim r as RangeDim q as WorksheetDim z as Chart
About the only one we will use is the “Range”data type.
![Page 16: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/16.jpg)
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)
![Page 17: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/17.jpg)
![Page 18: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/18.jpg)
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 (F2)
![Page 19: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/19.jpg)
Program module (Sub or Function) is made up of a series of steps to accomplish a task. Five major categories of steps:
Assignment statements A = 5
Conditional statements If ( A > 5) then ….
Calls to other modules A = sqrt (12)
Input /Output operations(Which are really calls to other modules)
Read 5, A
Iteration Statements For I = 1 to 6 … Next i
![Page 20: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/20.jpg)
Expression evaluation inVBA
Operators in priority orderLeft to right
Operator Priority List ( )^- (unary minus)* /+ -
Comparison Operators (>, < …)
Logical Operators (NOT, AND, OR…)
![Page 21: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/21.jpg)
Test = 2 + 3 ^2 > 5 AND (3-7)*2 > 6
(3-7) -43^2 9-4*2 -89+2 1111 > 5 True-8 > 6 FalseTrue AND False False
Thus, Test has the value “False”
![Page 22: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/22.jpg)
Conditional Expressions
If / then / else end if
Select case …. End Case
![Page 23: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/23.jpg)
If (expression) then
One or more expressions
Else
One or more expressions
End If
If (expression ) then (expression)
Note: no End If statement for this type of If /then expression
![Page 24: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/24.jpg)
Select Case testexpression[Case expressionlist-n[statements-n]] ...
[Case Else[elsestatements] ]
End Select
Select Case Statement
Like a complex If / Then / ElseIf … / EndIf
![Page 25: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/25.jpg)
Select Case Statement
Testexpression can be any that evaluates to a numeric value or a string
Expressionlist is compared to the testexpression to determine if the following statements should beexecuted. See “Help” for all the allowable forms
Statements are executed up to the next Case statement or the End Select statement
![Page 26: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/26.jpg)
A simple example
Case statements to determine a letter grade from a numeric grade. Assume the numeric grade is in the variable “grade”
![Page 27: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/27.jpg)
Select Case gradeCase Is >=90
ltrgrade=“A”Case Is >= 85
ltrgrade = “BA”…Case Is >= 70
ltrgrade = “C”Case Else
ltrgrade=“E”End Select
Note: Goes to first Case that testexpression satisfies. Is implicitly uses grade in the comparison
![Page 28: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/28.jpg)
Generally speaking, anything you can do with a Select Case block, you can do with a set of If / Then / Else If … / End If statements
![Page 29: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/29.jpg)
Iteration Statements Used to execute a number of statements
repeatedly
Two major types ---For / Next Do / Loop
![Page 30: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/30.jpg)
For counter to upperlimt step sizeStatements
Next counter
For Each cell In Selection Statements
cell.value= expression
Next cell
![Page 31: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/31.jpg)
Dostatements
Loop
Basic “ Do Loop” Structure
But we need a mechanism to stop the loop!
![Page 32: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/32.jpg)
Do while expression_is_true StatementsLoop
Do Statements
Loop while expression_is_true
![Page 33: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/33.jpg)
Do Until expression_is_true StatementsLoop
Do Statements
Loop Until expression_is_true
![Page 34: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/34.jpg)
Do...Loop Statement ExampleThis example shows how Do...Loop statements can be used. The inner Do...Loop statement loops 10 times, sets the value of the flag to False, and exits prematurely using the Exit Do statementThe outer loop exits immediately upon checking the value of the flag.
Dim Check, Counter Check = True Counter = 0 ' Initialize variables. Do ' Outer loop.
Do While Counter < 20 ' Inner loop. Counter = Counter + 1 ' Increment Counter. If Counter = 10 Then ' If condition is True.
Check = False ' Set value of flag to False. Exit Do ' Exit inner loop.
End If Loop Loop Until Check = False ' Exit outer loop immediately
![Page 35: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/35.jpg)
To manipulate a cell’s value, use the “value” property of the cell object:
Range(“A4”).value = 20Temp = Range(“A4”).value
orCells(4,5).value = 20Temp = Cells(4,5).value
![Page 36: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/36.jpg)
Build a simple module to set a 5 x 5 array of cells to some value
In VBA IDEInsert moduleInsert procedure / sub
Initcells
Dim I as integerDim J as integerFor I = 1 to 5For J = 1 to 5Cells(I,J) = I*JNext JNext I
Return to Excel and run macro Initcells
![Page 37: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/37.jpg)
Two useful VBA functions to interact with use:
Str1 = InputBox(“prompt’)
When invoked, displays the prompt and gives you a box to enter a reply, which is placed in Str1
Dim str1 as Stringstr1 = InputBox(“Please enter your name”)
InputBox displays a prompt and awaits a reply
![Page 38: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/38.jpg)
MsgBox displays a message and buttons to press. The function value returned is the code for the button
J = MsgBox(“prompt”,buttoncodes, “ box name”)
Look at ‘Help” for the button code valuesJ is the integer value of the button code the user presses
Code=MsgBox(“Are you OK”,vbOKOnly,”My Temp”)
![Page 39: Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something](https://reader036.vdocuments.net/reader036/viewer/2022062905/5a4d1ae87f8b9ab059979ad3/html5/thumbnails/39.jpg)
Write a simple sub (called Heater) that accepts a temperature as an argument and returns a string to indicate what to do with the heater. Also have MsgBox display a message to the use if the heater status is changed.
The logic for the heater control is as follows:
If the temperature is less than 65, “Turn heater on”If the temperature is greater than75, “Turn heater off”Otherwise, “Leave heater alone”
Function Heater (Temp) As stringCode
End function