Please note javascript is required for full website functionality.

Blog

VBA Blogs: 'Do' it 'Until' You Believe

13 April 2018

Last week this blog looked at using Do…Loop loops and how flexible they are at moving when or where to check the condition is makes them superior While Wend.

The final reason that Do…Loop is superior is that While Wend loops is that we can replace While with Until.  

What effect does this achieve? This essentially reverses the value of the condition to be tested. 

While executes the block of code when the condition is true and keeps executing that till the condition becomes false.  Once the condition becomes false, the loop is terminated.

Until does the opposite. It executes the block of code when the condition is false and keep executing that till the condition becomes true. Once the condition becomes true, the until loop is terminated.

In our Jack and Jill example, the condition is that Jack and Jill have not fallen down.  Our variable that determines that FallenDown = False in order to use Do While.  Knowing that changing the keyword While to Until essentially flips the Boolean test condition requirement, we would simply check against the condition FallenDown = True.

But note, our FallenDown variable is already a Boolean type. We don't even need to use the condition FallenDown = True because it is superfluous. We can convert our original loop as thus:

Hope everyone enjoyed reading our VBA series on Loops!

Loop back next week where we'll start looking at searching values in VBA!

Newsletter