Objective:
The objective of this document is to understand the use of the cycle For.in VBA there are three types of cycles:
For Next
Do While
Do Until
What is a cycle
Doing the exact same thing several times
For example, if I have 3 dirty plates, what would be my washing procedure?
Option 1
Take dirty plate 1
Scrub dirty plate 1
Rinse dirty plate 1
Dry dirty plate 1
Take dirty plate 2
Scrub dirty plate 2
Rinse dirty plate 2
Dry dirty plate 2
Take dirty plate 3
Scrub dirty plate 3
Rinse dirty plate 3
Dry dirty plate 3
Now imagine it being 50 plates.
What does the process mentioned before have in common? What do you observe is repeated? Let’s put a color to what is repeated.
Take dirty plate 1
Scrub dirty plate 1
Rinse dirty plate 1
Dry dirty plate 1
Take dirty plate 2
Scrub dirty plate 2
Rinse dirty plate 2
Dry dirty plate 2
Take dirty plate 3
Scrub dirty plate 3
Rinse dirty plate 3
Dry dirty plate 3
You can observe there are three cycles, this means the same steps are repeated three times, the only thing that changes is the plate number.
There is a representation for the cycle in code:
From plate=1 until 3 Take dirty plate
Scrub dirty plate
Rinse dirty plate
Dry dirty plate
Next plate
Summary
1) Before you program, generate your solution for the problem, like the cycle for dish washing, each instruction written independently. You don’t have to identify the pattern right away.
2) When you have your solution, identify the pattern that is repeated, you might have to adjust the instructions sometimes so that they are similar or depend on a variable.
3) Generate the pseudocode of the cycle.
4) Get ready to program.
Example of obtaining a cycle
You have been asked to generate a code to calculate the total funds that have been raised at a charity event.
You have to ask how many people have donated money, how much each one donated and give the final result.
TRY IT!!