OzGrid

Do While Loop, Repeat a Block of VBA Macro Code

< Back to Search results

 Category: [Excel]  Demo Available 

Do While Loop

 
 

Do While Loop, Repeat a Block of VBA Macro Code

Got any Excel Questions? Free Excel Help

Do While Loop

There are 2 ways a Do While loop can be used in Excel VBA Macro code. You can tell Excel to check the condition before entering the loop, or tell Excel to enter the loop and then check the condition. To stop an endless loop, press ESC or CTRL+BREAK. To force an exit from a Do Until loop you use the line: Exit Do, e.g. If lNum = 7 Then Exit Do

In the first example (Check_First) the condition (lNum>10) is checked before entering the loop.

In the second example (Check_After) the condition (lNum>10) is checked after entering the loop.

Sub Check_First()



Dim lCount As Long



Dim lNum As Long







    lCount = 0



    lNum = 11



    Do While lNum > 10



        lNum = lNum - 1



        lCount = lCount + 1



    Loop



    MsgBox "The Do While loop made " & lCount & " loop(s)."



End Sub











Sub Check_After()



Dim lCount As Long



Dim lNum As Long







    lCount = 0



    lNum = 9



    Do



        lNum = lNum - 1



        lCount = lCount + 1



    Loop While lNum > 10



    MsgBox "The Do While loop made " & lCount & " loop(s)."



End Sub

Do Loops

Syntax
Do
 [{While | Until} condition]
[statements]
[Exit Do]
[statements]
Loop

Or, you can use this syntax:
Do

[statements]
[Exit Do]
[statements]
Loop [{While | Until} condition]

 

Free Training Course: Lesson 1 - Excel Fundamentals

See also:

Debug Excel VBA Macro Code
Excel VBA - Excel Macros
Delete Module After Running VBA Code
Deleting Excel Named Ranges
Delete Excel Sheet Event Code Macro
Delete Excel Workbook Event Code
Matching ComboBox Controls

 

See also: Index to Excel VBA Code; Index to Excel Freebies; Lesson 1 - Excel Fundamentals; Index to how to… providing a range of solutions

 

Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.

Hookup Women apps, or "hook up girls" as they are called, will help you with the task of meeting women. The program AdultFriendFinder allows you to set up a profile and then it allows you to search the database for compatible women. It's like having a personal shopper, because you can easily search for women that you would like to meet. This program will also let you view other profiles and see which one you think is a good match. If you are looking to meet the perfect woman, this is probably one of the best apps to try out.

Gallery



stars (0 Reviews)