Is Operator

Description

Used to compare two object reference variables.

Syntax

result = object1 Is object2

The Is operator syntax has these parts

Part

Description

result

Required; any numeric variable.

object1

Required; any object name.

object2

Required; any object name.


Remarks

If object1 and object2 both refer to the same object, result is True; if they do not, result is False. Two variables can be made to refer to the same object in several ways.

In the following example, A has been set to refer to the same object as B:

Set A = B
The following example makes A and B refer to the same object as C:

Set A = C
Set B = C
See Also

Comparison operators, Operator precedence.

Example

This example uses the Is operator to compare two object references. The object variable names are generic and used for illustration purposes only.

Dim MyObject, YourObject, ThisObject, OtherObject, ThatObject, MyCheck
Set YourObject = MyObject            ' Assign object references.
Set ThisObject = MyObject
Set ThatObject = OtherObject
MyCheck = YourObject Is ThisObject    ' Returns True.
MyCheck = ThatObject Is ThisObject    ' Returns False.
' Assume MyObject <> OtherObject
MyCheck = MyObject Is ThatObject    ' Returns False.
Example (Microsoft Excel)

This example selects the intersection of two named ranges ("rg1" and "rg2") on Sheet1. If the ranges don't intersect, the example displays a message.

Worksheets("Sheet1").Activate
Set isect = Application.Intersect(Range("rg1"), Range("rg2"))
If isect Is Nothing Then
    MsgBox "Ranges do not intersect"
Else
    isect.Select
End If
This example finds the first occurrence of the word "Phoenix" in column B on Sheet1 and then displays the address of the cell that contains this word. If the word isn't found, the example displays a message.

Set foundCell = Worksheets("Sheet1").Columns("B").Find("Phoenix")
If foundCell Is Nothing Then
    MsgBox "The word was not found"
Else
    MsgBox "The word was found in cell " & foundCell.Address
End If