2. Intro to VBA Programming
Agenda
Variables
Types
Declaration
Assignment
Functions/Subroutines
Parameters
Returning values
Loops
While
For
3. Intro to VBA Programming
Agenda
Variables
Types
Declaration
Assignment
Functions/Subroutines
Parameters
Returning values
Loops
While
For
4. Intro to VBA Programming
You‟ve
seen variables before
X
=5
Y=X+3
Variables
can be reused and changed
X
=5
X=2
X=X+7
The
same principles apply in
programming
Variables
Functions
Loops
5. Intro to VBA Programming
Computers
are stupid
Must
tell them exactly what to do
The more we tell them, the faster they run
Each
language does this differently
We
will focus on VBA
Variables
Functions
Loops
6. Intro to VBA Programming
All
variables should be declared first
This
will prevent hard to find errors
It
is not required that you do so, but HIGHLY
encouraged
To
force yourself, use “Option Explicit” at the
beginning of VBA files
Variables
Functions
Loops
7. Intro to VBA Programming
To
declare a variable in VBA, the basic
structure is:
Dim <name> as <type>
Ex. Dim X as Integer
Variables
Functions
Loops
8. Intro to VBA Programming
Dim
means “new Dimension”
Fancy
VBA way of saying new variable
When
declaring a variable, you should
know what it will be used for
Different
variable types are used to store
different data
Variables
Functions
Loops
9. Intro to VBA Programming
Types
include:
Integer
(whole numbers like 3)
Float (decimal numbers like 1.25)
Double (just like float, can store wider range
of values)
Characters (like „a‟)
String (a number of characters in a row such
as “Ivey”)
MANY
Variables
more
Functions
Loops
10. Intro to VBA Programming
Types
include:
Integer
(whole numbers like 3)
Float (decimal numbers like 1.25)
Double (just like float, can store wider range
of values)
Characters (like „a‟)
String (a number of characters in a row such
as “Ivey”)
MANY
Variables
more
Functions
Loops
11. Intro to VBA Programming
To
assign a value to a use the “=“
operator
Ex.
Dim x As Integer (declare the variable)
x=5
Variables
Functions
Loops
12. Intro to VBA Programming
Agenda
Variables
Types
Declaration
Assignment
Functions/Subroutines
Parameters
Returning values
Loops
While
For
13. Intro to VBA Programming
Functions
Take
in input, process it, and produce
output
Variables
Functions
Loops
14. Intro to VBA Programming
Functions
General
must also be declared
format of function declaration:
Function <name>(ByVal <parameterName> As <type>) As <returnType>
Subroutines
are declared the same way,
but do not have a return type
Variables
Functions
Loops
15. Intro to VBA Programming
ByVal
refers to “by value”
This
will make a copy of the variable and its
value
ByRef
could also be used
This
Don‟t
will modify the original variable
worry too much about this for now
Variables
Functions
Loops
16. Intro to VBA Programming
To
explicitly return a value from a
function, assign the return value to the
function name itself
Variables
Functions
Loops
17. Intro to VBA Programming
Subroutines
are the same as functions,
except for 2 important differences:
1.
They do not have explicit return types
2.
They cannot be used directly in the
spreadsheet
Variables
Functions
Loops
18. Intro to VBA Programming
Agenda
Variables
Types
Declaration
Assignment
Functions/Subroutines
Parameters
Returning values
Loops
While
For
19. Intro to VBA Programming
Loops
Loops
allow us to repeatedly perform the
same task easily
There
are different kinds of loops
including:
While
Until
For
Variables
Functions
Loops
20. Intro to VBA Programming
“While”
true
“Until”
true
loops while a certain condition is
loops until a certain condition is
“For”
loops for a specified number of
iterations
Variables
Functions
Loops