Tutorial Cycle for VBA

Topics: Control flow, Programming language, For loop Pages: 6 (1326 words) Published: February 25, 2014
Tutorial: CycleFor in VBA
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!! AT LEAST COMPLETE THE ALGORITHM AND SEE THE SOLUTION AFTERWARDS!! IF YOU GET STUCK, GO TO THE SOLUTION PART AT THE END, AND TRY TO FIGURE IT OUT!! SOLUTION

Step 1
Generate the solution process without a cycle.
Input
numPeople = Number of people that donated
Amount = Amount donated by person
Output
Total = sum of all the amounts donated by person
Process
Ask for number of people (numPeople)
For 1 person, ask amount
Total = amount
Find out if everyone is accounted for, if they are, print out the total, if they are not then you should continue. For person 2, ask amount
Total = Total + amount (this means I’m accumulating what I had plus what is given to me) Find out if everyone is accounted for, if they are print out the total, if they are not then you should continue. For person 3, ask amount

Total = Total + amount (this means I’m accumulating what I had plus what is given to me) Find out if everyone is accounted for, if they are print out the total, if they are not then you should continue.

Maybe you are seeing a pattern, if you are then go ahead to Step 2. Step 2
Identify the pattern that is repeated. Observe the next patterns that are almost the same. For 1 person, ask amount
Total = amount
Find out if everyone is accounted for, if they are print out the total, if they are not then you should continue. For person 2, ask amount
Total = Total + amount (this means I’m accumulating what I had plus what is given to me) Find out if everyone is accounted for, if they are print out the total, if they are not then you should continue. For person 3, ask amount

Total = Total + amount (this means I’m accumulating what I had plus what is given to me) Find out if everyone is accounted for, if they are print out the total, if they are not then you should continue.

Now, you try to adjust or modify the patterns so that they are equal and you can turn them into a cycle. Total = amount

It can be the same as Total = Total + amount, if total is...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Essay about tutorial
  • Vba Introduction Essay
  • Essay about Tutorial
  • Cycle Essay
  • THUMB Tutorial Essay
  • Tutorial Essay
  • Tutorials Essay
  • Tutorial Essay

Become a StudyMode Member

Sign Up - It's Free