Go To Statements

Go-To Statements


Basic construct of Go To Statements

The most straightforward way to change the flow of a program. A VBA procedure can contain any number of labels, but cannot branch outside of a procedure. Not an example of good programming, and should only be used for error handling.

[sourcecode language=”vb” light=”true” wraplines=”false” gutter=”false”]
Sub GoToDemo()
UserName = InputBox(“Enter Your Name: ”)
If Username <> “Howard” Then GoTo WrongName
MsgBox (“Welcome Howard…”)
‘ [More Code Here]
Exit Sub
WrongName:
MsgBox “Sorry. Only Howard can run this.”
End Sub
[/sourcecode]