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

Any numeric variable.

object1

Any object name.

object2

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, Set Statement.

Example

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


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.

This example selects the intersection of two named ranges, rg1 and rg2, on Sheet1. If the ranges do not 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 is not found, the example diplays 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