Creating Object Variables
Last modified: December 06, 2011
Applies to: Office 2013 | VBA
To create an object variable:
Declare the object variable.
Assign the object variable to an object.
Use the Dim statement or one of the other declaration statements (Public, Private, or Static) to declare an object variable. A variable that refers to an object must be a Variant, an Object, or a specific type of object. For example, the following declarations are valid:
' Declare MyObject as Variant data type. Dim MyObject ' Declare MyObject as Object data type. Dim MyObject As Object ' Declare MyObject as Font type. Dim MyObject As Font
If you use an object variable without declaring it first, the data type of the object variable is Variant by default.
You can declare an object variable with the Object data type when the specific object type is not known until the procedure runs. Use the Object data type to create a generic reference to any object.
If you know the specific object type, you should declare the object variable as that object type. For example, if the application contains a Sample object type, you can declare an object variable for that object using either of these statements:
Dim MyObject As Object ' Declared as generic object. Dim MyObject As Sample ' Declared only as Sample object.
Declaring specific object types provides automatic type checking, faster code, and improved readability.
Use the Set statement to assign an object to an object variable. You can assign an object expression or Nothing. For example, the following object variable assignments are valid:
Set MyObject = YourObject ' Assign object reference. Set MyObject = Nothing ' Discontinue association.
You can combine declaring an object variable with assigning an object to it by using the New keyword with the Set statement. For example:
Set MyObject = New Object ' Create and Assign
Setting an object variable equal to Nothing discontinues the association of the object variable with any specific object. This prevents you from accidentally changing the object by changing the variable. An object variable is always set to Nothing after closing the associated object so you can test whether or not the object variable points to a valid object. For example:
If Not MyObject Is Nothing Then ' Variable refers to valid object. . . . End If
Of course, this test can never determine with absolute certainty whether or not a user has closed the application containing the object to which the object variable refers.
Use the Me keyword to refer to the current instance of the object where the code is running. All procedures associated with the current object have access to the object referred to as Me. Using Me is particularly useful for passing information about the current instance of an object to a procedure in another module. For example, suppose you have the following procedure in a module:
Sub ChangeObjectColor(MyObjectName As Object) MyObjectName.BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256) End Sub
You can call the procedure and pass the current instance of the object as an argument using the following statement: