Ti
tle:="Input Name")
If userName = "" Then
done = False
Else
done = True
End If
Wend
End Sub
In this procedure, we use the variable
done
to determine whether we are fin ished
loop ing. The
If
block could have all been in cluded in the
While
state ment, elim i-
nating the need for
done
. This would have made for short VBA code, but it would
have been very dif ficult to un derstand, par ticularly if the stop ping con dition was
more and more complicated. Setting up an
If
block allows you to check as many
conditions as you like and set
done
based on those con ditions. Then, the only
ques tion for
While
is: Are we done or not? If we are not done, keep loop ing.
Do
Loops
Do
loops are sim ilar to
While
loops. They al low you to specify either a
While
con dition (keep go ing while something is
True
) or an
Un
til
con di tion
(keep go ing un til something is
True
). They also let you specify the con dition
(ask the stopping question) at the beginning or the end. If the condition is at the
beginning, the loop might never run (not even once). If the con dition is at the
end, the loop will al ways run at least once. Here are some simple examples:
Do
an
swer = InputBox("How many plan
ets are in the so
lar sys
tem?")
Loop Un
til an
swer = "9"
Do
an
swer = InputBox("How many plan
ets are in the so
lar sys
Do'stlaringiz bilan baham: |