Ozgrid, Experts in Microsoft Excel Spreadsheets
Javascript DHTML Drop Down Menu Powered by dhtml-menu-builder.com
Learn how to create Excel dashboards.

Do Until Loop

TRY OUT: Smart-VBA | Code-VBA | Analyzer-XL | Downloader-XL | Trader-XL | More Free Downloads.. Best Value: Finance Templates Bundle

More Excel Video Tutorials

Do Until Loop, Repeat a Block of VBA Macro Code

Back To: Excel VBA Loops . Lot's of free Excel VBA . Got any Excel Questions? Free Excel Help

Do UntilLoop

There are 2 ways a Do Until 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. Note  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 = 10

    Do Until 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 = 11

    Do

        lNum = lNum - 1

        lCount = lCount + 1

    Loop Until lNum = 10

    MsgBox "The Do Until 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]

New & Less Than You Think: List Managers | Working With Excel Sheets In VBA | Excel Charting Lessons | Delete rows by condition | TRY OUT: Smart-VBA | Code-VBA | Analyzer-XL | Downloader-XL | Trader-XL | More Free Downloads.. Best Value: Finance Templates Bundle

Excel Dashboard Reports & Excel Dashboard Charts 50% Off Become an ExcelUser Affiliate & Earn Money

Special! Free Choice of Complete Excel Training Course OR Excel Add-ins Collection on all purchases totaling over $64.00. ALL purchases totaling over $150.00 gets you BOTH! Purchases MUST be made via this site. Send payment proof to special@ozgrid.com 31 days after purchase date.



Instant Download and Money Back Guarantee on Most Software

Try out: Analyzer XL | Downloader XL | Smart VBA | Trader XL Pro (best value) | ConsoXL | MergeXL | O2OLAP for Excel | MORE>>

Excel Trader Package Technical Analysis in Excel With $139.00 of FREE software!

Microsoft ® and Microsoft Excel ® are registered trademarks of Microsoft Corporation. OzGrid is in no way associated with Microsoft

GIVE YOURSELF OR YOUR COMPANY 24/7 MICROSOFT EXCEL SUPPORT & QUESTIONS