End Statement

See Also4US5FH0              ExampleGU368M>Low

Ends a Visual Basic procedure or block.

Syntax

End [{ Function | If | Select | Sub | Type}]

Remarks

You can use the End statement in any of the following ways:

Statement               Description

 

End Function          Ends a Function procedureK6LBMC definition.  You must use End Function with Function.  Automatically added when you create a new Function procedure.

End If                      Ends a block If...Then statement.  Required to end a block
If...Then...Else.

End Select              Required to end a Select Case block.

End Sub                  Required to end a Sub procedureQDBVHN.  Automatically added when you create a new Sub procedure.

End Type                Required to end a user-defined type definition (Type statement).

End                         By itself, terminates program execution.  Equivalent to choosing End from the  Run menu.  Never required and may be placed anywhere in a procedure.

 

While the End statement is never required to terminate program execution, it is generally accepted as good programming practice since it closes files, destroys forms, and clears the value of all variables.

The End statement does not trigger either the QueryUnload or the Unload event.


See Also

Exit Statement8NCB0T2

Function StatementEZ33BF

If...Then...Else StatementLANIF

Select Case StatementVCC4T3

Stop Statement4K739NO

Sub StatementLANSUB

Type StatementGUBAD0


End Statement Example

The example uses End to end code execution, to end a Select Case block, and to end a Sub procedure.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

 

Sub Form_Click ()

   Dim UserInput                   ' Declare variable.

   Do                              ' Set up infinite loop.

      UserInput = UCase(InputBox("Enter A or X to end program."))

      Select Case UserInput        ' Evaluate input.

         Case "A" : MsgBox "You entered 'A'."

         Case "X" : End            ' End execution if user entered X.

         Case Else : MsgBox "You made an invalid choice. Try again."

      End Select                   ' End Select Case block.

   Loop

End Sub                            ' End Sub procedure.