PDA

View Full Version : Solved: Do ... loop



Nader
04-04-2008, 07:38 AM
I know there are four different syntaxes of Do...Loop
1- Do While condition [statements] Loop
2- Do [statements] Loop While condition
3- Do Until condition [statements] Loop
4- Do [statements] Loop Until condition

I only know to dela with the number 3

RowCount = 1
Do Until Cells(RowCount, 1).Value = 5
RowCount = RowCount + 1

Loop
Cells(RowCount, 1) = 10

in this code the loop will show error if there isn't 5 number in the column 1 and if ther is a 5 will continue.

May somone help me to understand other loop(1,2,4)

MikeO
04-04-2008, 08:17 AM
To do the same thing with Number 1:
RowCount = 1
Do While Cells(RowCount, 1).Value <> 5
RowCount = RowCount + 1

Loop
Cells(RowCount, 1) = 10

Number 2:

RowCount = 0
Do
RowCount = RowCount + 1
Loop While Cells(RowCount, 1).Value <> 5
Cells(RowCount, 1) = 10

Number 4:

RowCount = 0
Do
RowCount = RowCount + 1
Loop Until Cells(RowCount, 1).Value = 5
Cells(RowCount, 1) = 10

Nader
04-04-2008, 10:54 AM
What a bout While..Wend .

MikeO
04-04-2008, 11:19 AM
What a bout While..Wend .

"While...wend" is essentially the same as "Do While condition [statements] Loop". For your example it would be:




RowCount = 1
While Cells(RowCount, 1).Value <> 5
RowCount = RowCount + 1
Wend
Cells(RowCount, 1) = 10


But the "Do While...Loop" method is more useful in some situations because you can use "Exit Do" to jump out of the loop. For example, if you only want to look down 100 rows for the number 5 (in your example) you would have this:

RowCount = 1
Do While Cells(RowCount, 1).Value <> 5
RowCount = RowCount + 1
If RowCount > 100 Then Exit Do
Loop
Cells(RowCount, 1) = 10


"While....Wend" doesn't have an Exit statement.

Nader
04-05-2008, 02:01 AM
Thank you for help.