Defining Object Variables

 < Day Day Up > 



One of the strengths of VBA is that you are not limited to creating simple variables that hold data assigned from another variable or derived from a calculation. In fact, if you want to create a new worksheet, a chart, or any other 'thing' in Excel that is represented as an object in the Excel VBA object model, you can do so by declaring an object variable. Using the Dim statement, you provide a name for the variable and then indicate the type of object you wish to create, as in the following code fragment, which creates a reference to a worksheet:

Dim myObj as Worksheet

Using the object variable is very different from using normal variables, mainly because object variables are pointers to an object, not the object itself. That is, you haven't actually created a new worksheet yet-all you've done is tell VBA that you want the myObj variable to hold a Worksheet object. Therefore, you need to actually provide the object you want assigned to the variable, which you accomplish using the Set command. The following command sets the object variable myObj to point to the first worksheet:

Set myObj = Worksheets(1)



 < Day Day Up > 



Microsoft Excel 2003 Programming Inside Out
Microsoft Office Excel 2003 Programming Inside Out (Inside Out (Microsoft))
ISBN: 0735619859
EAN: 2147483647
Year: 2006
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net