The GoTo Statement

A GoTo statement offers the most straightforward means for changing a program's flow. The GoTo statement simply transfers program control to a new statement, which is preceded by a label.

Your VBA routines can contain as many labels as you like. A label is just a text string followed by a colon. (See Chapter 7 for more label information.)

The following procedure shows how a GoTo statement works:

Sub GoToDemo()

UserName = lnputBox("Enter Your Name: ") If UserName <> "Bill Gates" Then GoTo WrongName MsgBox ("Welcome Bill...") ' ...[More code here] ...

Exit Sub WrongName:

MsgBox "Sorry. Only Bill Gates can run this." End Sub

The procedure uses the InputBox function to get the user's name. If the user enters a name other than Bill Gates, the program flow jumps to the WrongName label, displays an apologetic message, and the procedure ends. On the other hand, if Mr. Gates signs on, the procedure displays a welcome message and then executes some additional code (not shown in the example). The Exit Sub statement ends the procedure before the second MsgBox function has a chance to work.

This simple routine works, but VBA provides several better (and more structured) alternatives than GoTo. In general, you should use GoTo only when you have no other way to perform an action. In practice, the only time you really need to use a GoTo statement is for trapping errors. (I cover this in Chapter 12.)

What is structured programming? Does it matter?

If you hang around with programmers, sooner or later you hear the term structured programming. This term has been around for decades, and programmers generally agree that structured programs are superior to unstructured programs. So, what is structured programming? And can you do that using VBA?

The basic premise of structured programming is that a routine or code segment should have only one entry point and one exit point. In other words, a block of code should be a stand-alone unit. A program cannot jump into the middle of this unit, nor can it exit at any point except the single exit point. When you write structured code, your program progresses in an orderly manner and is easy to follow — unlike a program that jumps around in a haphazard fashion. This pretty much rules out using the GoTo statement.

In general, a structured program is easier to read and understand. More important, it's also easier to modify when the need arises.

VBA is indeed a structured language. It offers standard structured constructs such as If-Then-Else, For-Next loops, Do-Until loops, Do-While loops, and Select Case structures. Furthermore, it fully supports module code constructions. If you're new to programming, you should try to develop good structure programming habits early on. End of lecture.

Many hard-core programming types have a deep-seated hatred for GoTo statements because using them tends to result in difficult-to-read "spaghetti code." Therefore, you should avoid this subject when talking with other programmers.

0 0

Post a comment