Skip to main content

End statement

Table of contents
  1. Syntax
  2. Remarks
  3. Examples

Ends a procedure or block.


End Function
End If
End Property
End Select
End Sub
End Type
End With

Statement Description
End Terminates execution immediately. Never required by itself but may be placed anywhere in a procedure to end code execution, close files opened with the Open statement, and to clear variables.
End Function Required to end a Function statement.
End If Required to end a block If…Then…Else statement.
End Property Required to end a Property Let, Property Get, or Property Set procedure.
End Select Required to end a Select Case statement.
End Sub Required to end a Sub statement.
End Type Required to end a user-defined type definition (Type statement).
End With Required to end a With statement.


When executed, the End statement resets all module-level variables and all static local variables in all modules. To preserve the value of these variables, use the Stop statement instead. You can then resume execution while preserving the value of those variables.

The End statement provides a way to force your program to halt. For normal termination of a Visual Basic program, you should unload all forms. Your program closes as soon as there are no other programs holding references to objects created from your public class modules and no code executing.


This example uses the End statement to end code execution if the user enters an invalid password.

Sub Form_Load()
    Dim Password, Pword
    Password = "Swordfish"
    Pword = InputBox("Type in your password")
    If Pword <> Password Then
        MsgBox "Sorry, incorrect password"
    End If
End Sub

Leave a comment

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

Format your code: <pre><code class="language-vba">place your code here</code></pre>