Microsoft Office Tutorials and References
In Depth Information
5.4.8.1 The generic As Object declaration
General objects:
Comment
Font
Range
Workbook
FillFormat
Outline
Sheets
Worksheet
Filter
PageSetup
Window
WorksheetFunction
Thus, we can declare variables such as:
Dim wb As Workbook
Dim wks As Worksheet
Dim chrt As Chart
Dim ax As axis
Dim pf As PivotField
We will devote much of this topic to studying the objects in the Excel object model, for it is
through these objects that we can manipulate Excel programmatically.
5.4.8.1 The generic As Object declaration
It is also possible to declare any Excel object using the generic object data type Object , as in the
following example:
Dim chrt As Object
While you may see this declaration from time to time, it is much less efficient than a specific
object declaration, such as:
Dim chrt As Chart
This is because Excel cannot tell what type of object the variable chrt refers to until the program
is running, so it must use some execution time to make this determination. This is referred to as
late binding and can make programs run significantly more slowly. (For more on late versus early
binding, see Appendix E . ) Thus, generic object declarations should be avoided.
We will discuss object variables in some detail in Chapter 9 . However, we should briefly discuss
the Set statement now, since it will appear from time to time in upcoming code examples.
5.4.8.2 The Set statement
Declaring object variables is done in the same way as declaring nonobject variables. For instance,
here are two variable declarations:
Dim int As Integer ' nonobject variable declaration
Dim chrt As Chart ' object variable declaration
On the other hand, when it comes to assigning a value to variables, the syntax differs for object
and nonobject variables. In particular, we must use the Set keyword when assigning a value to an
object variable. For example, the following line assigns the currently active Excel chart to the
variable chrt :
Set chrt = ActiveChart
Search JabSto ::




Custom Search