In our prior tutorial, we covered how to make a standard Do loop. As we adjusted our code throughout the tutorial we ended up telling our loop to stop at our first empty cell.
Do Until IsEmpty(ActiveCell)
This loop is considered a Do Until Loop. The setup of the loop continues to process our series of statements in a loop until a set condition is met, at which point the loop ends.
There is one other version of the Do Loop which we will be covering in this tutorial, the Do While Loop. The Do While Loop allows you specify a certain condition to be met, and continue looping as long as that condition is true. As soon as the condition is false, the loop ends.
An easy way to demonstrate this is to start with the integer 1 and increment it by 1 for each loop. Let’s say I want to only loop while my integer is less than 10.
- Create our Sub called Do_While_Loop().
- Define you variable as an integer (1,2,3,4 etc) I use “i” in this case
- Start your integer off at the value of 0
- Create your “Do While i is less than 10” Loop
- Inside of the loop, increment i by 1
You code will now loop continuously until i = 10
Sub Do_While_Loop() Dim i As Integer i = 0 Do While i < 10 i = i + 1 Loop End Sub
If you liked this content and found it helpful, please like the page and leave a comment below. You can also ask questions on the Forum to get personalized help on your issues and questions.