The syntax of Exit Sub and Exit Function is similar to that of Exit For and Exit Do in the previous section, “Exiting a Control Structure.” Exit Sub can appear as many times as needed, anywhere within the body of a Sub procedure. Exit Function can appear as many times as needed, anywhere within the body of a Function procedure.
Exit Sub and Exit Function are useful when the procedure has done everything it needs to do and can return immediately. For example, if you want to change the previous example so it prints only the first common Printer and Screen font it finds, you would use Exit Sub:
Private Sub Form_Click()
Dim SFont, PFont
For Each SFont In Screen.Fonts()
For Each PFont In Printer.Fonts()
If SFont = PFont Then
Print Sfont
Exit Sub ' Exit the procedure.
End If
Next PFont
Next SFont
End Sub
No comments:
Post a Comment