Upload
axel-relefors
View
226
Download
3
Embed Size (px)
DESCRIPTION
Excel Vba Lecture One
Citation preview
Introduc)on to Engineering Compu)ng GEEN 1300
17th April 2013
Visual Basic for Applica/ons (VBA)
Announcements
• Project specific email addresses posted on D2L • Project specific help hours posted on D2L
• Read chapter 13 on VBA Func/ons
Project Selec/on
4
Excel workbook
worksheetswith cells
chart sheetswith graphs
VBA Project
moduleswith proceduresand functions
Option AOption BOption C
OKOption AOption BOption C
OKOK
Option AOption BOption C
OKOption AOption BOption C
OKOK
userformswith associated
VBA code
Excel Application
click on the Excel buHon on the toolbar
5
VBA program units: Subrou)nes and Func)ons
Subs: • lumps of VBA code that can be executed by running it from Excel, from the VBE
• input & output typically via reading/changing cells
• can be created with the macro recorder or typed in from scratch
Sub name (op/onal arguments) End Sub
• may have zero-‐to-‐several arguments that provide for values to be input to the Sub
6
• func/ons return values in place of their names
• lumps of VBA code that can be executed by using their names in a VBA expression or an Excel formula
Func)ons:
• func/ons cannot be recorded, but must be typed into the VBE
• func/ons may have zero-‐to-‐several input arguments
Func/on name (op/onal arguments)
End Func/on
7
Examples:
Function Triple(mynumber)
Triple = 3 * mynumber
End Function
Sub hello()
MsgBox ("Hello")
End Sub
8
How to create these: (see sec/on 13.3.2)
Insert a module
Which will plop this into
your VBA window:
9
How to create these:
Then insert a procedure:
When you choose “Sub” or “Func/on” from this dialog, VBA will plop down the corresponding “wrapper” in your module, which you can then edit.
10
Proper)es Window
Project Explorer
Code Window
11
How does VBA code get created? 1) Generally, you write it from scratch with the VBE
(“visual basic editor”)
2) Excel also writes VBA code automa/cally when you run the macro recorder
then you usually use the VBE to clean up and modify the automa5cally-‐wri9en code
12
Object-‐oriented Programming
Objects : en//es that have aHributes (“proper/es”) that can be manipulated
These are generally organized in hierarchies:
Microso` Office
Word
Excel
“Applica/on” Objects in red
Range(“A1”)
The object that corresponds to cell A1
13
Proper)es, Methods and Events proper/es are a9ributes of objects methods are ac5ons to be taken on objects events are happenings that objects respond to
Examples: Range(“A1”).Width
Range(“B2”).Clear
property
method
There are thousands of Objects, Proper/es, Methods, and Events in VBA, but there are only a few that you’ll use all the /me.
So, don’t be afraid of drowning in a sea of these things.
Open event (workbook is opened)
14
Range Object proper)es
Value read/write (R/W) what’s stored in the cell
Text
[ default: it’s what you get if you don’t specify a property ]
a formaHed string of what’s displayed in the cell
read-‐only (R)
Count no. of cells in a range read-‐only (R)
Column Row
index number of column or row of single-‐cell range read-‐only (R)
Address absolute address of cell or range read-‐only (R)
15
Range object methods
Select select a cell or range of cells
Copy Paste
Clear erase contents of the cell
Delete delete the cell or range, shi`ing other cells into place
Range(“C1:D4”).Select Selec/on.Copy Range(“A1”).Select Ac/veSheet.Paste
Range(“C1”).Copy Range(“D1”) or
16
VBA program units: Subrou)nes
A VBA subrou)ne…
A. returns a value and has only one input argument B. returns a value and has several input arguments C. returns a value and has no input arguments D. does not return a value and takes only one input argument
E. None of the above
17
VBA program units: Subrou)nes
A VBA subrou)ne can be executed by…
A. typing it into an Excel cell and pressing ENTER B. opening it in the VBE and pressing the play buHon C. typing it into the Excel formula bar and pressing ENTER
D. using it inside of a func/on E. using the Macro buHon on the Developer toolbar
18
Func)ons:
A VBA func/on returns an output argument by…
A. using the return keyword before the value to return B. using the return keyword with a ‘=‘ before the value to
return (e.g., return = 4 * 4) C. using the func/on name followed by a ‘=‘ and then the
value to return D. using return and then the func/on name, followed by
the value to return E. Ha! Trick ques/on, func/ons cannot return a value
19
Examples:
Function Twice(mynumber)
Twice = 2 * mynumber
End Function
Sub hello()
‘MsgBox “Hello” would work here too
MsgBox ("Hello")
End Sub
20
Examples:
Function Icy(temp) If (temp <= 32) Then MsgBox ("Yes") Icy = temp
End Function
21
Naming Rules for Subs and Func)ons
Permissible characters: leHers, numbers, some punctua/on Must start with a leHer
Cannot look like a cell reference
Can’t use #, $, %, &, or !
Can’t use spaces or periods
< 255 characters (!)
VBA does not dis/nguish between upper and lowercase, but the first style entered will be “remembered”
Use reasonably short names that carry a clear meaning
Temperature may be beHer than Temp or T
Mole_Frac)on or MoleFrac)on both good
22
VBA “Nuts and Bolts”
1. Data types
The most common: single, integer and string
Single used for most numbers with decimal frac/ons and possibly exponents precision: about 6-‐to-‐7 significant figures range: from about 1038 down to 10-‐39, both + and -‐
Integer used for most coun/ng numbers
range: from –32,768 to 32,767
uses 4 bytes of memory
uses 2 bytes of memory
23
String used to store text
1 byte is used for each character characters are stored according to the standard ASCII code
Addi)onal data types
Double used for numbers with decimal frac/ons and possibly exponents where high precision or extended range are required precision: about 15-‐to-‐16 significant figures
range: from about 10308 down to 10-‐309, both + and -‐ uses 8 bytes of memory
Important: Excel uses “double” for the numbers stored in cells on the spreadsheet
24
Long used for integers where extended range is required range is about -‐ 2 billion to + 2 billion each number uses 4 bytes of memory
used for T/F informa/on
two constant values: TRUE and FALSE 2 bytes are used for each Boolean quan/ty
Boolean (also called “logical”)
Object
Variant
used for any defined object takes up 4 bytes of memory
adjusts automa/cally to the data type required
“chameleon” data type
storage requirements vary
25
Variables are iden/fiers for storage loca/ons for one or more data types.
It is useful to think of a variable as the name on the outside of a mailbox, wherein one or more values can be stored.
When a data type is associated with a variable name, the mailbox can only store informa/on of that type.
VBA “Nuts and Bolts” 2. Variables
The values stored in variables can change... Temperature = 43
Temperature = 37
The Temperature “mailbox” stores 43, un/l 37 is “assigned” to it, wiping out the 43, and then storing the 37
2 = Temperature … illegal!