Lesson 39 of 60 – Exit Statements in VBA
65%

Exit Statements in VBA

Exit statements in VBA are used to stop or leave a procedure, function, loop, or other VBA structure before it reaches its normal ending point.

Exit statements are especially useful when you want to stop a loop early, leave a Sub procedure, or stop processing when a particular condition occurs.

Note: Different Exit statements are used for different VBA structures. For example, Exit Sub exits a Sub procedure, while Exit For exits a For loop.

1. What is an Exit Statement?

An Exit statement tells VBA to leave a particular programming structure immediately.

For example:

Exit Sub

This statement immediately exits the current Sub procedure.

2. Why Use Exit Statements?

Exit statements are useful when the remaining code should not be executed.

  • Stop a loop early.
  • Exit a Sub procedure.
  • Exit a Function.
  • Exit a Property procedure.
  • Stop processing when an error or condition occurs.
  • Improve control over VBA programs.

3. Types of Exit Statements

VBA provides different Exit statements for different structures.

  • Exit Sub – exits a Sub procedure.
  • Exit Function – exits a Function procedure.
  • Exit Property – exits a Property procedure.
  • Exit For – exits a For loop.
  • Exit Do – exits a Do loop.

4. Exit Sub

Exit Sub is used to leave a Sub procedure immediately.

Sub Test()

    MsgBox "Start"

    Exit Sub

    MsgBox "End"

End Sub

The second MsgBox will not execute because the procedure exits before reaching it.

5. Exit Sub with If Statement

Exit Sub is often used with an If statement.

Sub CheckMarks()

    If Range("A1").Value = "" Then

        MsgBox "Enter marks first"

        Exit Sub

    End If

    MsgBox "Marks are available"

End Sub

If A1 is empty, the procedure stops immediately.

6. Exit Sub for Data Validation

Exit Sub can prevent further processing when required information is missing.

Sub SaveStudent()

    If Range("A2").Value = "" Then

        MsgBox "Enter Student Name"

        Exit Sub

    End If

    MsgBox "Student Saved"

End Sub

This is useful in data-entry programs.

7. Exit Function

Exit Function immediately leaves a Function procedure.

Function CheckNumber(ByVal number As Integer) As String

    If number < 0 Then

        Exit Function

    End If

    CheckNumber = "Positive Number"

End Function

When the number is negative, the function exits before assigning the result.

8. Exit Function with a Condition

Exit Function is useful when a function should stop processing under a particular condition.

Function GetGrade(ByVal marks As Integer) As String

    If marks < 0 Or marks > 100 Then

        Exit Function

    End If

    If marks >= 40 Then
        GetGrade = "Pass"
    Else
        GetGrade = "Fail"
    End If

End Function

9. Exit For

Exit For is used to immediately leave a For...Next or For Each loop.

Dim i As Integer

For i = 1 To 10

    If i = 5 Then
        Exit For
    End If

    MsgBox i

Next i

The loop stops when i becomes 5.

10. Exit For with Student Records

Exit For can stop searching when a required student is found.

Dim i As Integer

For i = 2 To 20

    If Cells(i, 1).Value = "Rahul" Then

        MsgBox "Student Found"

        Exit For

    End If

Next i

Once Rahul is found, there is no need to continue the search.

11. Exit For with For Each

Exit For also works with a For Each loop.

Dim cell As Range

For Each cell In Range("A1:A20")

    If cell.Value = "Rahul" Then

        MsgBox "Found"

        Exit For

    End If

Next cell

12. Exit Do

Exit Do is used to immediately leave a Do loop.

Dim i As Integer

i = 1

Do While i <= 10

    If i = 5 Then
        Exit Do
    End If

    MsgBox i

    i = i + 1

Loop

The loop stops when i reaches 5.

13. Exit Do with Do While

Exit Do can be used inside a Do While loop when an additional stopping condition is required.

Dim i As Integer

i = 1

Do While i <= 10

    If Cells(i, 1).Value = "Stop" Then
        Exit Do
    End If

    i = i + 1

Loop

The loop stops when the word Stop is found.

14. Exit Do with Do Until

Exit Do can also be used inside a Do Until loop.

Dim i As Integer

i = 1

Do Until i > 10

    If i = 5 Then
        Exit Do
    End If

    MsgBox i

    i = i + 1

Loop

The loop exits when i reaches 5, even though the original Do Until condition has not yet been reached.

15. Exit Property

Exit Property is used to leave a Property procedure.

Property Get StudentName() As String

    If Range("A1").Value = "" Then
        Exit Property
    End If

    StudentName = Range("A1").Value

End Property

It is mainly used when working with VBA property procedures.

16. Exit Statement with Validation

Exit statements are useful for validating input before continuing.

Sub AddStudent()

    If Range("A2").Value = "" Then

        MsgBox "Enter Student Name"

        Exit Sub

    End If

    If Range("B2").Value = "" Then

        MsgBox "Enter Course"

        Exit Sub

    End If

    MsgBox "Student Added"

End Sub

17. Exit Statement and Error Checking

An Exit statement can prevent a procedure from continuing when required data is invalid.

Sub CalculateResult()

    If Range("B2").Value < 0 Then

        MsgBox "Invalid Marks"

        Exit Sub

    End If

    MsgBox "Calculation Started"

End Sub

18. Exit For vs Exit Do

The two statements are used with different types of loops.

Statement Used With
Exit For For...Next and For Each loops
Exit Do Do While and Do Until loops

19. Exit Sub vs Exit Function

Different procedure types use different Exit statements.

Statement Purpose
Exit Sub Leaves a Sub procedure.
Exit Function Leaves a Function procedure.
Exit Property Leaves a Property procedure.

20. Exit For in a Search Program

A common practical use of Exit For is stopping a search after finding the required record.

Sub SearchStudent()

    Dim i As Integer

    For i = 2 To 100

        If Cells(i, 1).Value = "Amit" Then

            MsgBox "Student Found in Row " & i

            Exit For

        End If

    Next i

End Sub

21. Exit Do in a Data Processing Program

Exit Do can stop processing when a special value is found.

Sub ProcessData()

    Dim rowNumber As Integer

    rowNumber = 2

    Do While Cells(rowNumber, 1).Value <> ""

        If Cells(rowNumber, 1).Value = "STOP" Then
            Exit Do
        End If

        Cells(rowNumber, 2).Value = "Processed"

        rowNumber = rowNumber + 1

    Loop

End Sub

22. Multiple Exit Statements

A procedure can contain more than one possible Exit statement.

Sub StudentCheck()

    If Range("A2").Value = "" Then
        MsgBox "Enter Name"
        Exit Sub
    End If

    If Range("B2").Value = "" Then
        MsgBox "Enter Marks"
        Exit Sub
    End If

    MsgBox "Data is Complete"

End Sub

Each Exit Sub handles a different validation condition.

23. Exit and Nested Loops

When using nested loops, an Exit statement exits the loop in which it is placed.

Dim i As Integer
Dim j As Integer

For i = 1 To 3

    For j = 1 To 5

        If j = 3 Then
            Exit For
        End If

        Cells(i, j).Value = j

    Next j

Next i

Here, Exit For exits the inner For loop.

24. Exit and Student Result Processing

Exit For can stop processing when an invalid mark is detected.

Sub CheckMarks()

    Dim i As Integer

    For i = 2 To 20

        If Cells(i, 2).Value < 0 Or Cells(i, 2).Value > 100 Then

            MsgBox "Invalid Marks in Row " & i

            Exit For

        End If

    Next i

End Sub

25. Practical Data Validation Example

Exit Sub can be used to stop a student data-entry program when required information is missing.

Sub SaveStudent()

    If Range("A2").Value = "" Then

        MsgBox "Enter Student Name"

        Exit Sub

    End If

    If Range("B2").Value = "" Then

        MsgBox "Enter Student ID"

        Exit Sub

    End If

    If Range("C2").Value = "" Then

        MsgBox "Enter Course"

        Exit Sub

    End If

    MsgBox "Student Saved Successfully"

End Sub

26. Common Mistakes with Exit Statements

Beginners commonly make these mistakes:

  • Using Exit For outside a For loop.
  • Using Exit Do outside a Do loop.
  • Using Exit Sub when a Function should return a value.
  • Placing Exit statements in the wrong condition.
  • Exiting a loop before completing required processing.
  • Using too many Exit statements without clear logic.

27. Practical Search Example

The following example searches for a student and stops as soon as the student is found.

Sub FindStudent()

    Dim i As Integer

    For i = 2 To 50

        If Cells(i, 1).Value = "Ravi" Then

            MsgBox "Ravi found in row " & i

            Exit For

        End If

    Next i

End Sub

This avoids continuing through the remaining rows after the student is found.

28. Best Practices for Exit Statements

  • Use Exit statements for clear and meaningful conditions.
  • Use the correct Exit statement for the current structure.
  • Validate input before performing important operations.
  • Use Exit For when a search result has already been found.
  • Use Exit Do when a special condition requires a loop to stop.
  • Avoid unnecessary Exit statements.
  • Keep the program logic easy to understand.

29. Complete Exit Statement Workflow

A typical workflow is:

  1. Start a Sub, Function, or loop.
  2. Process the required data.
  3. Check a condition.
  4. If the condition requires early termination, use the appropriate Exit statement.
  5. VBA immediately leaves that structure.
  6. Continue with the code after the structure, if applicable.
For i = 1 To 10

    If i = 5 Then
        Exit For
    End If

    MsgBox i

Next i

30. Complete Understanding of Exit Statements

Exit statements provide additional control over VBA programs. They allow you to leave a procedure or loop before it reaches its normal ending point.

The most important Exit statements for beginners are:

  • Exit Sub – exits a Sub procedure.
  • Exit Function – exits a Function procedure.
  • Exit Property – exits a Property procedure.
  • Exit For – exits a For loop.
  • Exit Do – exits a Do loop.
Sub Example()

    Dim i As Integer

    For i = 1 To 10

        If i = 5 Then
            Exit For
        End If

        Cells(i, 1).Value = i

    Next i

End Sub

In this example, the For loop stops when i = 5. Exit statements are especially useful for validation, searching, data processing, and controlling loops.

The next lesson will introduce Nested Loops, where one loop is placed inside another loop.

📌 Key Points

  • Exit statements stop a VBA structure before its normal ending.
  • Exit Sub exits a Sub procedure.
  • Exit Function exits a Function procedure.
  • Exit Property exits a Property procedure.
  • Exit For exits a For or For Each loop.
  • Exit Do exits a Do While or Do Until loop.
  • Exit statements are useful for validation and searching.
  • Exit statements can stop unnecessary processing.
  • Always use the Exit statement appropriate to the current VBA structure.

🧠 Quick Quiz

Question: Which Exit statement is used to immediately leave a For loop?