Table of contents
Collapse the table of content
Expand the table of content

End Statement

office 365 dev account|Last Updated: 6/12/2017
1 Contributor

Ends a procedure or block.



EndFunctionEndIfEnd PropertyEnd SelectEnd SubEnd TypeEnd With The End statement syntax has these forms:

EndTerminates 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 clearvariables.
End FunctionRequired to end a Function statement.
End IfRequired to end a block If…Then…Else statement.
End PropertyRequired to end a Property Let, Property Get, or Property Set procedure.
End SelectRequired to end a Select Case statement.
End SubRequired to end a Sub statement.
End TypeRequired to end a user-defined type definition ( Type statement).
End WithRequired to end a With statement.

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

Note The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Code you have placed in the Unload, QueryUnload, and Terminate events offorms andclass modules is not executed. Objects created from class modules are destroyed, files opened using the Open statement are closed, and memory used by your program is freed. Object references held by other programs are invalidated.

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" 
 EndEnd IfEnd Sub
© 2018 Microsoft