VBA - Week 3 Guide
VBA - Week 3 Guide
VBA - Week 3 Guide
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.
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
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 link:
• https://www.youtube.com/watch?v=vFhTFMM5BM4&list=PLWPirh4EWFpEFSYTbKaST6hSlg
IFCJjU3&index=55
• https://www.youtube.com/watch?v=cfzhUQj9s7c