VBA Do Loop

Looping structures like the VBA Do Loop allow you to specify the conditions under which you would like your program to run a set of code statements repeatedly.

There are 4 different variations of the VBA Do Loop we are going to look at:

‘Do While…Loop structures check the condition before running the code.
‘The code in the loop is run only if the condition evalutes as True, and
‘repeats until the test expression evaluates as False…Example…

Private Sub DoWhile()
    Dim intItems As Integer
    
    Do While intItems < 25
        MsgBox "You have to order a minimum of 25. " & _
            "You have ordered " & intItems & " so far."
    
        intItems = intItems + InputBox("How many more items do you need?")
    Loop
    
    MsgBox "That's the correct amount."
    
    
End Sub

‘Do Until…Loop structures check the condition before running the code.
‘The code in the loop is run only if the condition evaluates as False,
‘and repeats until the test expression evaluates as True…Example…

Public Sub DoUntil()
    Dim strPassword As String
    Dim strCorrectPassword As String
    
    strCorrectPassword = "pass123"
    
    'Make sure you ask the question once, or else the user will go directly to the invalid message, _
        and probably get confused.
    strPassword = InputBox("Enter the password")
     
    Do Until strPassword = strCorrectPassword
        MsgBox "That's not the correct password, try again."
        strPassword = InputBox("Enter the password")
        
    Loop
    
    MsgBox "Welcome to the site!"
    
End Sub

‘Do…Loop While structures check the condition after running the code and
‘repeat a code block until the test expression evaluates as False…Example…

Public Sub DoLoopWhile()
    Dim strAnswer As String
    
    Do
        MsgBox "You must answer the question, before continuing: "
        strAnswer = InputBox("4 + 6 / 2 = ?")
    
    Loop Until strAnswer = "7"
    
    MsgBox "Correct"
    
End Sub

‘Do…Loop Until structures check the condition after running the code and
‘repeat a code block until the test expression evaluates as True.


Public Sub DoLoopUntil()
    Dim strSearchText As String
    Dim intPos As Integer
    
    'Useful for parsing csv, or other delimited files...
    
    strSearchText = "1990, Cadillac, Seville, Gold"
    intPos = 0
    
    Do
        intPos = InStr(intPos + 1, strSearchText, ",")
        MsgBox "Comma at position: " & intPos
        
    Loop Until intPos = 0
End Sub

Watch the video:

Exit a VBA Do Loop by using the Exit Do statement.

<< VBA Error Handling PT2 | VBA For Loop >>


 




By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How To Parse A Flat File In Excel VBA

In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]

How can I interact with other Office applications (Excel) using VBA in Access?

Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]

How To Generate A XML File With Access VBA

XML is used to structure your data. You can send a CSV file to another person, but using XML, you can structure your data in a more elegant format, and get very specific about your fields, and what data is contained within your tags (your field names). This is different than a basic CSV file […]

Here Is A Customized Msgbox VBA Example

Here is an example of a customized VBA Msgbox. We giving some richtext and customizable flair to the rather ordinary message box. The following code provided with the code after the screenshot, is going to provide you with the ability to really make the ordinary message box shine! Sub MsgboxVBAExamples() Dialog.Box “VBAHowTo.com is your source […]

Previous Post

VBA For Loop

Next Post

VBA Error Handling PT2

Leave a Reply

Your email address will not be published. Required fields are marked *