Вы находитесь на странице: 1из 24

COMP1022Q

Introduction to Computing with Excel VBA

Introduction to Looping
David Rossiter, Gibson Lam and Eddie Chan

What is Looping?
A loop is a set of code which repeats many times
Looping is a very useful feature in programming
languages such as VBA because it makes repetitive
work easier
For example, you can use a nested
loop to generate a chess board
In this presentation we will look
at two types of loops:
While Loops
Do Loops

WhileWend
While . . .condition. . .
. . .statement(s). . .
Wend
While condition is true, repeatedly execute
statement(s)
When condition is false, WhileWend finishes

COMP1022Q

Introduction to Looping

Page 3

The Flow of WhileWend

Is condition true?
yes

Execute
statement(s)

no

An Example of WhileWend 1/2


' Select the first cell in the 4th row
Cells(4, 1).Select
Loop condition

While ActiveCell.Row <= 10


' Put the row number into the first cell
ActiveCell.Value = "I am row " & _
ActiveCell.Row
Loop
body

' Move the selection down one cell


ActiveCell.Offset(1, 0).Select
Wend

This line of code selects the


cell on the row following the
currently selected cell

An Example of WhileWend 2/2

COMP1022Q

Introduction to Looping

Page 6

Running the WhileWend Example 1/10


' Select the first cell in the 4th row
Cells(4, 1).Select
While ActiveCell.Row <= 10
' Put the row number into the first cell
ActiveCell.Value = "I am row " & _
ActiveCell.Row
' Move the selection down one cell
ActiveCell.Offset(1, 0).Select
Wend

The first cell


in row 4 is
selected

Running the WhileWend Example 2/10


' Select the first cell in the 4th row
Cells(4, 1).Select
The condition is
true because
While ActiveCell.Row <= 10
the active cell is
' Put the row number into the first cell
in row 4 (<=10)
ActiveCell.Value = "I am row " & _
ActiveCell.Row
' Move the selection down one cell
ActiveCell.Offset(1, 0).Select
Wend

Running the WhileWend Example 3/10


' Select the first cell in the 4th row
Cells(4, 1).Select
While ActiveCell.Row <= 10
' Put the row number into the first cell
ActiveCell.Value = "I am row " & _
ActiveCell.Row
' Move the selection down one cell
ActiveCell.Offset(1, 0).Select
Wend

Running the WhileWend Example 4/10


' Select the first cell in the 4th row
Cells(4, 1).Select
While ActiveCell.Row <= 10
' Put the row number into the first cell
ActiveCell.Value = "I am row " & _
ActiveCell.Row
' Move the selection down one cell
ActiveCell.Offset(1, 0).Select
Wend
The cell on
the next row
is selected

Running the WhileWend Example 5/10


' Select the first cell in the 4th row
Cells(4, 1).Select
The condition
is true because
While ActiveCell.Row <= 10
the active cell
' Put the row number into the first cell
is now in row 5
ActiveCell.Value = "I am row " & _
ActiveCell.Row
' Move the selection down one cell
ActiveCell.Offset(1, 0).Select
Wend

Running the WhileWend Example 6/10


' Select the first cell in the 4th row
Cells(4, 1).Select
While ActiveCell.Row <= 10
' Put the row number into the first cell
ActiveCell.Value = "I am row " & _
ActiveCell.Row
' Move the selection down one cell
ActiveCell.Offset(1, 0).Select
Wend

Running the WhileWend Example 7/10


' Select the first cell in the 4th row
Cells(4, 1).Select
While ActiveCell.Row <= 10
' Put the row number into the first cell
ActiveCell.Value = "I am row " & _
ActiveCell.Row
' Move the selection down one cell
ActiveCell.Offset(1, 0).Select
Wend
The cell on
the next row
is selected

Running the WhileWend Example 8/10


Next
iteration

Next
iteration

Next
iteration

Next
iteration

Next
iteration

Running the WhileWend Example 9/10


' Select the first cell in the 4th row
Cells(4, 1).Select
The condition
is false when
While ActiveCell.Row <= 10
the active cell
' Put the row number into the first cell
is in row 11
ActiveCell.Value = "I am row " & _
ActiveCell.Row
' Move the selection down one cell
ActiveCell.Offset(1, 0).Select
Wend

Running the WhileWend Example 10/10


' Select the first cell in the 4th row
Cells(4, 1).Select
While ActiveCell.Row <= 10
' Put the row number into the first cell
ActiveCell.Value = "I am row " & _
ActiveCell.Row
' Move the selection down one cell
ActiveCell.Offset(1, 0).Select
Wend
Since the condition is false the contents
of the loop do not get executed, and
the loop finishes

Do WhileLoop
Do While . . .condition. . .
. . .statement(s). . .
Loop
The usage of Do WhileLoop is exactly the same as
WhileWend
The difference is that the keywords of Do
WhileLoop are more English than WhileWend
COMP1022Q

Introduction to Looping

Page 17

The Flow of Do WhileLoop


This flowchart is the
same as the flowchart
of WhileWend

Is condition true?
yes

Execute
statement(s)

no

Example of Do WhileLoop
For example, we can create a program which does the same
thing as the WhileWend example using Do WhileLoop:
' Select the first cell in the 4th row
Cells(4, 1).Select
Loop condition
Do While ActiveCell.Row <= 10
' Put the row number into the first cell
ActiveCell.Value = "I am row " & _
ActiveCell.Row
Loop
body
' Move the selection down one cell
ActiveCell.Offset(1, 0).Select
Loop

DoLoop While
Do
. . .statement(s). . .
Loop While . . .condition. . .
This is similar to the previous two loops we looked at
but condition is evaluated after statement(s) is
executed
This means that statement(s) will be executed at least
once before condition is evaluated
COMP1022Q

Introduction to Looping

Page 20

The Flow of DoLoop While

Execute
statement(s)

yes

Is condition true?
no

An Example of DoLoop While 1/3


' Execute the loop at least once
Do
' Ask a question
Loop
Answer = InputBox("Do you think
body
comp1022q is a great course?")
' Check the answer at the end of the loop
Loop While LCase(Answer) <> "yes"
Loop condition

The LCase() method changes all letters in some


text into lower case letters, e.g.:
LCase("BIG")  "big"
LCase("How Are You")  "how are you"

An Example of DoLoop While 2/3


You need to answer a
question in an InputBox

If you answer No you


will be asked again

If you answer Not at all!


you will be asked again

An Example of DoLoop While 3/3


If you answer I dont
think so you will be
asked again

You will not be asked the


question again if you
answer Yes

In this example, if you answer Yes, yes, YES


they will all stop the InputBox from showing up again

Вам также может понравиться