Do Until Loops

Do-Until Loops

The Do Until loop structure is very similar to the Do While structure. The difference is evident only when the condition is tested. In a Do While loop, the loop executes while the condition is True; in a Do Until loop, the loop executes until the condition is True.


Do Until loops have two syntaxes:

[sourcecode language=”vb” light=”true” wraplines=”false” gutter=”false”]
Do [Until condition]
[instructions]
[Exit Do]
[instructions]
Loop
[/sourcecode]

or

[sourcecode language=”vb” light=”true” wraplines=”false” gutter=”false”]
Do
[instructions]
[Exit Do]
[instructions]
Loop [Until condition]
[/sourcecode]


Do Until Test at Beginning:

[sourcecode language=”vb” light=”true” wraplines=”false” gutter=”false”]
Sub EnterDates3()
Dim TheDate As Date

TheDate = DateSerial(Year(Date), Month(Date), 1)

Do Until Month(TheDate) <> Month(Date)
ActiveCell = TheDate
TheDate = TheDate + 1
ActiveCell.Offset(1, 0).Activate
Loop
End Sub
[/sourcecode]


Do Until Test at End:

[sourcecode language=”vb” light=”true” wraplines=”false” gutter=”false”]
Sub EnterDates4()
Dim TheDate As Date

TheDate = DateSerial(Year(Date), Month(Date), 1)

Do
ActiveCell = TheDate
TheDate = TheDate + 1
ActiveCell.Offset(1, 0).Activate
Loop Until Month(TheDate) <> Month(Date)
End Sub
[/sourcecode]