• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

11. Functions and Subroutines > 11.4. Exiting a Procedure

Exiting a Procedure

VBA provides the Exit Sub and Exit Function statements, should we wish to exit from a procedure before the procedure would terminate naturally. For instance, if the value of a parameter is not suitable, we may want to issue a warning to the user and exit, as Example 11-5 shows.

Example 11-5. Using the Exit Sub statement
Sub DisplayName(sName As String)
   If sName = "" then
      Msgbox "Please enter a name."
      Exit Sub
   End If 
   MsgBox "Name entered is " & sName
End Sub

While we are on the subject of exiting, we should comment on the use of the End statement, which will terminate a procedure. Simply put, you should almost never use the End statement in VBA programming, since it produces a rather abrupt termination of a program. (I never like to say never.) Here is a partial list of what happens when the End statement is executed:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint