You’ll run into objects, methods, and properties ‘thrqughout VBA.If you’ve looked atthe AutoCAD object model, you’ve already run across all-three of these: .” An object is.any type of variable that hes complex beha~. For example, the Document object in AutoCAD represents an entire draWing file.
• A method is anything !hat an object knows hoW to do. The methods of the Document object include the open and Regen methods. You can think of methods as the verbs for an object.,
• A property Is anything that describes an object. The properties Qf the Document object include the Name and ActiveLinetype properties. You”an think of properties’ as the adjectives describing an object. ” In addition to the objects provided by AutoCAD, VBAhas a number of built-in objects, ‘including the collection object you’ve just seen, Later in this chapter you’ll learn how to ‘ use class modules to’ creste your own. objects:
Although the original BASIC language lacked many features, most of those deficiencies have been remedied in modem versions of BASIC, such as VBA. VBA is now a full-fledged structured programming language, with a full set of control structures for looping, branching, and flow of control. This section will describe the control structures that are most often useful in applications.
The If … Then… Else Structure
To handle branching, VBA uses an If …Then …Else structure, as shown in following example:
In this structure, each If or El self keyword is followed by a Booleanconditionsomething that can be evaluated as either True or False. When VBA comes to a condition that evaluates as True, it executes the statements that follow that condition. If you include an El se clause, that set of statements is executed if none of the conditions is True. Note the End If statement that is required to terminate the conditional. the result of executing IfDemo with a variety of inputs.
The Select case Statement
Deeply nested If …Then…Else loops can be very difficult to decipher. Always beware of difficult code, because it’s a potien maintainability nightmare: If you need to modify such code, the chance of introducing a new error is all too large. Fortunately, VBA provides an alternative branching structure for such situations, the Select Case statement. Here’s an example of the Select Case statement syntax:
The Sel ect Case statement works by comparing the specified expression (in this example, intNum) to each case in turn, until it finds one that matches. At that point, the code in that particular case is executed. Here, the return value of the procedure is determined by the matching case. As you can See, the case can be a particular value, a range of values (3 to 5) or a collection of values (7, 9). You can use the Sel ect Case statement with any type of ~ariable. The values of the individual cases must be the same data type as the variable.
VBAprovides several looping structures for repetitively executing code. The For loop is perhaps the simplest of these, providing a way to execute a set of statements a predetermined number of times. The For loop works by incrementing or decremet} Png a counter
For loops are most useful when you know exactly how many times you need to execute the loop. Often, you’ll use a For loop to rocess the elements in an array. If you’re dealing with a collection, you may be better off using a Doloop, as shown in the next section.
Doloops provide an alternative to For loops when you don’t know how many times you need to execute the loop but know when you need to stop. The simplest Doloop executes a statement as long as some condition is True and stops as soon as it is False:
You can also cause a Doloop to be executed as long as a particular condition is False by using Until instead of While:
The GoTo Operator’
In addition to branching and looping, VBA includes an operator to transfer flow of control to another place in the program. This is the infamous GoTo,which :causes execution to resume at a different spot (a label) in the procedure:
Debug.Print ‘In the demo’
Debug.Print ‘This won’t print’
·Debug.Print ‘This wi 11 pr-int •
In this example, CoHere is a label, a nonexecutable statement that serves mark a place in the procedure. Although using a GoTostatement is often assooan with sloppy, hard-to-decipher programming, this statement is essential in error handling in VBA, as you’ll see later in this chapter. Otherwise, you can almostalways avoid GoTostatements in your code.