You are on page 1of 5

Introduction to VBA in Excel

Week 4
Hello Students and welcome to the next VBA steps you will be learning. Let’s begin.

Study items for this week


These are the topics you should familiarize yourself for this week:
1. Understanding different types of loops
2. Difference between procedures and functions

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.

1. Understanding different types of loops


Loops are special statements in VBA that allow you to run the same lines of code multiple
times. For example, If you have 10 cells, and you want to fill them up with data, instead of you
typing 10 times Cells(x,y).value = someValue, you can write the line of code once and then tell
VBA using loop statements to execute it as many times as you require.
In VBA there are these four types of loop construct:
1. For-Next loop – performs a specified number of iterations.
2. Do-While loop – performs iterations only while a conditional test made on each iteration
evaluates as True.
3. Do-Until loop – performs iterations only while a conditional test made on each
iteration evaluates as False.
4. For-Each loop – performs iterations to traverse each element of an array.
Go through the videos and articles for more details:
1. For Loop
• For-Next loop – performs a specified number of iterations.
For counter = start To end
Statement to execute
Statement to execute
Next counter

Watch the following video:


• https://www.youtube.com/watch?
v=kKonIb8SDoM&list=PLWPirh4EWFpEFSYTbKaST6hSlgIFCJjU3&index=49
• https://www.youtube.com/watch?
v=lOS_3u0VIxc&list=PLWPirh4EWFpEFSYTbKaST6hSlgIFCJjU3&index=50

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

Watch the video link:


• https://www.youtube.com/watch?v=n3-
oMBywAxo&list=PLWPirh4EWFpEFSYTbKaST6hSlgIFCJjU3&index=56

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

4. For each Loop


• For-Each loop – performs iterations to traverse each element of an array.
For Each item In collection
Statement to execute
Statement to execute
Statement to execute
Next item

Watch link:
• https://www.youtube.com/watch?
v=vFhTFMM5BM4&list=PLWPirh4EWFpEFSYTbKaST6hSlgIFCJjU3&index=55
• https://www.youtube.com/watch?v=cfzhUQj9s7c

For all these loops you can also look at article:


• https://www.excel-pratique.com/en/vba/loops
• https://www.tutorialspoint.com/vba/vba_loops.htm

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

2. Difference between sub procedures and functions


A Procedure is lines of code that you want VBA to execute together. There main two forms of
procedures in VBA knowns as ‘sub-procedure/sub-routine’ or ‘functions’.

‘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

Read more about functions in the link:


• https://www.tutorialspoint.com/vba/vba_functions.htm
• https://www.excel-pratique.com/en/vba/procedures_functions

Sub Procedures/ Sub routines:


Procedures and sub procedures (sub routines) do not return a value. You can give them inputs known as
‘arguments’ or ‘parameters’. The ‘sub/end sub’ keywords that you have been using so far means sub
procedure/sub routine.

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

You might also like