Please note javascript is required for full website functionality.


VBA Blogs: At Least Once in Our Lives, Let’s 'Do' It!

6 April 2018

Last week this blog looked at using Do…Loop loops and experimented with the Exit statement making them better than While Wend.

The second reason that Do…Loop is superior is that While Wend loops check for the condition prior to running – but with Do…Loop the condition can be checked at the end.  

Now why would you want to test the condition at the end?

For an example, Jack & Jill will always take at least one step onto the hill before falling. They won't fall if they haven't moved!

How this is done in code is by simply moving the “While condition” part of the Do statement next to Loop.

The syntax changes to:


[ statements ]

[ Exit Do ]

[ statements]

Loop [{ While |Until } condition ]

This will execute the statements within the loop to run at least once, before checking the condition and exiting if applicable.

Last week left Jack and Jill sitting pretty on the top of the hill having already taken 5 steps. Let’s set them on a loop walking, despite having reached the top.

Sub DoItAtLeastOnce()

Dim CurrentStepsTaken As Integer

CurrentStepsTaken = 5


    CurrentStepsTaken = CurrentStepsTaken + 1

Loop While CurrentStepsTaken < 5

Debug.Print CurrentStepsTaken & " steps were taken."

End Sub

This results in:

Loop back next week to find out the final attribute making Do…Loop superior to While Wend.