Set Statement

Description

Assigns an object reference to a variable or property.

Syntax

Set objectvar = {objectexpression | Nothing}

The Set statement syntax has these parts:

Part

Description

objectvar

Name of the variable or property; follows standard variable naming conventions.

objectexpression

Expression consisting of the name of an object, another declared variable of the same object type, or a function or method that returns an object of the same object type.

Nothing

Discontinues association of objectvar with any specific object. Assigning objectvar to Nothing releases all the resources associated with the previously referenced object when no other variable refers to it.


Remarks

To be valid, objectvar must be an object type consistent with the object being assigned to it.

The Dim, Private, Public, Redim and Static statements only declare a variable that refers to an object. No actual object is referred to until you use Set statement to assign a specific object.

Generally, when you use Set to assign an object reference to a variable, no copy of the object is created for that variable. Instead, a reference to the object is created. More than one object variable can refer to the same object. Because these variables are references to (rather than copies of) the object, any change is reflected in all variables that refer to it.

See Also

Dim Statement, Let Statement, Private Statement, Public Statement, ReDim Statement, Static Statement.

Example

This example adds a new worksheet to the active workbook and then sets the name of the worksheet.


Set newSheet = Worksheets.Add
newSheet.Name = "1995 Budget"

This example creates a new worksheet and then inserts a list of all names in the active workbook, including their formulas in A1-style notation in the language of the user.


Set newSheet = ActiveWorkbook.Worksheets.Add
i = 1
For Each nm In ActiveWorkbook.Names
    newSheet.Cells(i, 1).Value = nm.NameLocal
    newSheet.Cells(i, 2).Value = "'" & nm.RefersToLocal
    i = i + 1
Next