VBA - Week 4 Guide
VBA - Week 4 Guide
Week 4
Hello Students and welcome to the next VBA steps you will be learning. Let’s begin.
For each of these points we have mentioned some text, links to videos, book chapters etc. that
you can use to understand and practice with. You are welcome to look at other sources too for
better understanding. You are also encouraged to discuss these with your group members and
learn from each other.
By the time we get to the lab for this week you should develop a basic understanding of the
items listed above.
You can see the other examples 3,4,5 the person works with in related links.
2. Do While Loop
• Do-While loop – performs iterations only while a conditional test made on each
iteration evaluates as True.
Do While test-expression
Statement to execute
Statement to execute
updater (updates test value to false to stop looping)
Loop
3. Do Until Loop
• Do-Until loop – performs iterations only while a conditional test made on each
iteration evaluates as False.
Do Until test-expression
Statement to execute
Statement to execute
Updater (updates test value to true to stop looping)
Loop
Watch the video link:
• https://www.youtube.com/watch?
v=MdZ8gwv4NLI&list=PLWPirh4EWFpEFSYTbKaST6hSlgIFCJjU3&index=57
Watch link:
• https://www.youtube.com/watch?
v=vFhTFMM5BM4&list=PLWPirh4EWFpEFSYTbKaST6hSlgIFCJjU3&index=55
• https://www.youtube.com/watch?v=cfzhUQj9s7c
In addition to these links, please read the following topics of the book
(Excel_VBA_in_easy_steps_-_Mike_McGrath): (See Chapter 5)
1. Performing Loops
2. Looping while true
3. Breaking from loops
4. Iterating For each
5. Including with
‘We use procedures and functions to create modular programs. Visual Basic statements are
grouped in a block enclosed by Sub, Function and matching End statements. The difference
between the two is that functions return values, procedures do not.
Start by watching this video comparing the two:
https://www.youtube.com/watch?v=UmR1HGnLfvo
Functions:
A VBA “custom function” is a procedure that is similar in some respects to a subroutine
procedure. For example, a function can accept arguments in the same way as a subroutine. But
unlike a subroutine, a function can also return a value to the caller.
Like the different built-in excel functions of Average(), Vlookup() etc that you have been using, a
function has ‘arguments’ it can take as input and it can also return an output value where it was
called. A function cannot be created using ‘Record Macro’ and must be written down as code in
the VBA Developer.
Sytax:
Function function_name (argument list) As datatype
Statements to execute
Function_name = value
End Function
Watch these videos:
• https://www.youtube.com/watch?
v=j6hmdfu5A5U&list=PLWPirh4EWFpEFSYTbKaST6hSlgIFCJjU3&index=79
• https://www.youtube.com/watch?v=doyqjqkyIRI
• https://www.youtube.com/watch?v=5f-ks4fzW3Q
Since you are already familiar with creating and working with subroutines we haven’t shared
additional videos. But do read about passing arguments to them:
• https://www.excel-pratique.com/en/vba/procedures_functions
By default, sub procedures have global accessibility status. This means they can be called upon to
execute their statements, using the ’Call’ keyword followed by their name, from within any other
subroutine in the workbook.
• https://www.tutorialspoint.com/vba/vba_sub_procedure.htm
Passing arguments/parameters:
VBA procedures can be created to accept arguments/parameters when called, whose values
can then be used by its statements. The arguments can be any type of variable, constant,
expression, or a “literal” value such as a number or a string of text. It is important to recognize
that arguments can be passed to procedures in two distinctly different ways:
• By Reference – This is the default way of passing arguments. Passing a variable, for example,
internally passes the memory address of the variable. This means that any changes made by the
procedure are made to the original variable.
• By Value – This is a less common way of passing arguments. Passing a variable, for example,
internally passes only a copy of the original variable. This means that any changes made by the
procedure are not made to the original variable.
Arguments/parameters are normally placed in parenthesis when calling the procedure or
function:
• Sub procuedureName (argumentsName As datatype, arg2 As Integer, arg3 As String)
• Function functionName (argumentName As datatype, arg2 As Integer, arg3 As String) As
String
In addition to these links, please read the following topics of the book
(Excel_VBA_in_easy_steps_-_Mike_McGrath): (See portions of Chapter 6 and 7)
1. Calling subroutines
2. Passing arguments
3. Defining a function
4. Calling a function