March_7 - is executed. But after next i=11, bigger than 10,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
ECM Tutorials March 7-11, 2011 Review This tutorial we will learn the “for…next” loop in Excel VBA. A loop allows you to repeat  some action in a certain predetermined way. Let’s see a simple loop:                                 for i=1 to 10                                  line1                              next In this example, i is the counter variable, which will increase from 1 to 2, 3, … 10.  During the first iteration, i=1 is smaller than 10, so line1 will be executed. After executing  line1, the “next” sentence will add 1 on the counter variable so that i=2, and we return to  the beginning of the loop and start the second iteration. Since i=2 is still smaller than 10,  line1 will be executed again, and the loop continues. During the 10 th  iteration, i=10, line1 
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: is executed. But after next i=11, bigger than 10, so the loop stops. What happens if I need the counter i runs from 1 to 3, 5, 9. That is, I want i increases by 2 each time. You can add a step size in the for line: for i=1 to 10 step 2 line1 next In this case, i will start from 1, which is smaller than 10, so line1 will be executed. After next, i will be increased by 2 to 3, still smaller than 10, so line1 will be executed again. After five iterations, i will jump from 9 to 11, bigger than 10, so the loop stops. Exercises: Ex1: Write some VBA codes to calculate 1+2+3+4+5+10. Save your result in cell A1 in sheet2....
View Full Document

Ask a homework question - tutors are online