O'Reilly logo

Writing Word Macros, Second Edition by Steven Roman PhD

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exiting a Procedure

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

Example 6-6. 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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required