Using Specific Object Variables

The object variable can refer to any type of object. Because Visual Basic has many types of objects, to make your programs more readable and faster, it's a good idea to create object variables that refer to a particular type of object. For instance, in the UseObjVariable procedure, instead of the generic object variable (Object), you can declare the myRange object variable as a Range object:

Dim myRange As Range

If you want to refer to a particular worksheet, you can declare the Worksheet object:

Dim mySheet As Worksheet

Set mySheet = Worksheets("Marketing")

When the object variable is no longer needed, you can assign Nothing to it. This frees up memory and system resources:

Set mySheet = Nothing You will see additional examples of using object variables in Chapter 9.

0 0

Post a comment