How To Exit A VBA Loop

Hi, there are times when you need to exit a loop after a certain condition has been met.

How do you exit function in VBA?

In the following example, you are going to see how to exit a function in VBA:

Sub StartNumbers()
    Dim intNumber As Integer
    
    intNumber = ExitTest
    
    'the number is going to be 3
    MsgBox "The number is " & intNumber
    
End Sub

Function ExitTest() As Integer
    Dim intCounter As Integer
    
    For intCounter = 1 To 5
        If intCounter = 3 Then
            ExitTest = intCounter
            Exit Function
        End If
    Next
End Function

How do you stop loop in VBA?

In the following example you’ll discover how easy it is to exit the vba loop.

For the “For” loop just tell the code, to “Exit For”.

and for the “Do” Loop just tell the code to “Exit Do”.

Sub ExitForLoop()
    
    Dim intCount As Integer
    
    For intCount = 1 To 100
        
        Debug.Print intCount
        
        If intCount = 50 Then
            MsgBox "50 has been reached.  Now exiting...", , "Exit For"
            Exit For
        End If
    Next
    
End Sub

Sub ExitDoLoop()
    
    Dim intCount As Integer
    
    Do Until intCount = 100
        
        Debug.Print intCount
        
        If intCount = 50 Then
            MsgBox "50 has been reached.  Now exiting...", , "Exit Do"
            Exit Do
        End If
        
        intCount = intCount + 1
    Loop
    
End Sub

In some instances “Exit For” or “Exit Do” doesn’t work. In these cases try using the keyword “End” (check out this sample) or add a “Go To (something)” label, then exit the sub there.

Want to see a “Do WHILE LOOP” in Access VBA? Ciick me.

You are also going to find out:
What does a loop do in VBA?

Hopefully that helps someone. Let me know if you have any questions.

Click here for related posts on looping and exiting in VBA.





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...

What is the purpose of the Me keyword in Access VBA?

What does the Me keyword mean? “Me” refers to the Access form currently in focus. Instead of writing out the entire form reference, you can just use the keyword “Me” which is easier. Like: Me.txtbox = “I am a textbox on the form that currently has the focus.” or you can update a label’s caption […]

How To Do A VBA CSV Import For One To Multiple Files

So you want to be able to import your csv for 1 to multiple files. Here’s how you do it. 1. up a form with a button. 2. After you click the button, you will get a File Dialog Box that will show you the csv files to choose from: Here is the code behind […]

How To Have Access Export A Table To An Excel XLSX In VBA

This was regarding a question I received from this blog: Hello, I am hoping to find some help. I have an Access DB that I use to collect data. I have a number of different reporting features. I would like to add the option for my user to export to excel. When I do this […]

How To Find Certain Files In VBA

Someone wanted to loop through the files in over 100 directories and find a list of their powerpoint files. This code will loop a specific directory and check if a file with a “pptx” extension exists in that directory. If it does, a message box will be displayed. Instead of the message box, the file’s […]

Previous Post

Joke: Debugging Definition

Next Post

How To Make An Access Select Query