0% found this document useful (0 votes)
2 views7 pages

Excel VBA Programming - Do Loops

The document explains two types of Do loops in Excel VBA: Do While and Do Until. It provides examples of how to structure these loops, emphasizing the importance of incrementing loop variables to avoid infinite loops. Additionally, it discusses the use of Exit statements to break out of loops early.

Uploaded by

comfortmbozole25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Excel VBA Programming - Do Loops

The document explains two types of Do loops in Excel VBA: Do While and Do Until. It provides examples of how to structure these loops, emphasizing the importance of incrementing loop variables to avoid infinite loops. Additionally, it discusses the use of Exit statements to break out of loops early.

Uploaded by

comfortmbozole25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

21/03/2021 Excel VBA Programming ­ Do Loops


 
Home Excel
and LearnVBA

Excel VBA Do
Loops

There are two kinds of Do loops in Excel VBA, Do While and Do


Until. Let's take a look at the Do While loop 䙕rst.

The Do While Loop

The Do While loop is a lot easier to use than the For loop you
used in the previous lesson, as you don't need to set a start

https://www.homeandlearn.org/excel_vba_do_loops.html 1/7
21/03/2021 Excel VBA Programming ­ Do Loops

condition, just the end condition. Here's the structure of a Do


While Loop:

Do While [CONDITION]

Loop

After Do While on the 䙕rst line, you need a condition to test.


This is usually the same sort of thing you did in the Conditional
Logic section. For example:

Do While x < y

Do While counter < 5

Here, the loop goes round and round until the test condition at
the start evaluates to FALSE. As soon as VBA detects that your
test condition is FALSE it will exit the loop and continue on its
way.

However, you do need a line of code to increase your loop


variable, otherwise you'll create an in䙕nite loop. In the code
below, we're increasing (incrementing) the counter variable by 1
each time round the loop:

Do While counter < 5

counter = counter + 1

Loop

If we didn't increment the counter variable then there would be


no way for VBA to reach the end condition of "Do while counter
https://www.homeandlearn.org/excel_vba_do_loops.html 2/7
21/03/2021 Excel VBA Programming ­ Do Loops

is less than 5".

If you like, you can add your condition at the end, just after the
word Loop:

Do

counter = counter + 1

Loop While counter < 5

The di�erence here is that any code you have for you loop will
execute at least once, if the condition is at the end. If the
condition is at the start then the loop code won't get executed if
the condition already evaluate to FALSE (counter might not be
less than 5, in our code above).

Let's try an example, though. Add a new Sub to your coding


window from the previous lesson. Call it DoWhileExample. Now
add the following code

We've set up an Integer variable called counter, and set it to a


value of 1. The Do Loop goes round and round while counter is
less than 5. So as not to set up an in䙕nite loop, we have this line:

https://www.homeandlearn.org/excel_vba_do_loops.html 3/7
21/03/2021 Excel VBA Programming ­ Do Loops

counter = counter + 1

If you're having trouble understanding this line, just start to the


right of the equal sign to see what calculation is being
performed:

counter + 1

Whatever value is currently in counter will get 1 added to it. But


this value needs to be stored somewhere. The "somewhere" just
happens to be the counter variable again, which is on the left of
the equal sign. The old value inside of counter will be
overwritten, to be replaced with the new value that VBA has just
calculated on the right of the equal sign.

The other line in the code is this:

Cells(counter, "D").Value = counter

Because counter changes each time round the loop we can use
it in between the round brackets of Cells, for the Rows value.
The Column we've chosen is the D column.

Run your code and your spreadsheet will now look like this:

https://www.homeandlearn.org/excel_vba_do_loops.html 4/7
21/03/2021 Excel VBA Programming ­ Do Loops

So we have the numbers 1 to 4 in the D column.

And here's the times table programme again, but this time using
a Do While Loop. Give it a try:

Do Until Loop

A loop that's closely related to the Do While loop is the Do Until


loop. You use them the same way, except instead of typing
While you type Until. When you're using Until, though, the end
condition will change slightly. Here it is for the Do While Loop:

Do While counter < 5

counter = counter + 1

Loop

And here is the Do Until Loop with a condition on the end.

https://www.homeandlearn.org/excel_vba_do_loops.html 5/7
21/03/2021 Excel VBA Programming ­ Do Loops

Do Until counter > 5

counter = counter + 1

Loop

Now we're saying Do Until counter is greater than 5.

If you need to bail out early from any loop then you only need
the word Exit followed by the loop name:

Do Until counter > 5

If counter = 0 Then

Exit Do

Else

End If

Loop

The above applies to For loops, as well: just put Exit For if you
need to bail out early.

In the next section, we'll take a look at something called an


Array.

Arrays in Excel VBA >

https://www.homeandlearn.org/excel_vba_do_loops.html 6/7
21/03/2021 Excel VBA Programming ­ Do Loops

<< Back to the Excel VBA Home Page

Lots more free online courses here on our main Home and
Learn site

© All course material copyright Ken Carney

https://www.homeandlearn.org/excel_vba_do_loops.html 7/7

You might also like