VBA - Week 3 Guide

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Introduction to VBA in Excel

Week 3
Hello Students and welcome to the next VBA steps you will be learning. Again, the concepts
introduced this week are the foundations on which the rest of the VBA activities will be built on.
So please make sure you give this due time and focus. As always, the instructors and TA are
available to help and guide too.

Study items for this week


These are the topics you should familiarize yourself for this week:
1. Scope of the variables
2. Understanding Loops
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. Scope of Variables in VBA


The scope of a variable in Excel VBA determines where that variable may be used. You
determine the scope of a variable when you declare it. There are three scoping
levels: procedure level, module level, and public module level.

The following videos can help you understand the different types of variables and the scope of
the variables in VBA i.e. Dim vs. Static. vs. Public:
https://www.youtube.com/watch?v=lzxf48Mvvqo
https://www.youtube.com/watch?v=Lcxv0Bx3NVM
You can also read about this topic from the following website:
https://wellsr.com/vba/excel/vba-variable-scope/
2. 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

Watch the following video:


• https://www.youtube.com/watch?v=kKonIb8SDoM&list=PLWPirh4EWFpEFSYTbKaST6hSlgIF
CJjU3&index=49
• https://www.youtube.com/watch?v=lOS_3u0VIxc&list=PLWPirh4EWFpEFSYTbKaST6hSlgIFCJ
jU3&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=PLWPirh4EWFpEFSYTbKaST6hSlgIF
CJjU3&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

Watch link:
• https://www.youtube.com/watch?v=vFhTFMM5BM4&list=PLWPirh4EWFpEFSYTbKaST6hSlg
IFCJjU3&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

You might also like