The Is Operator

To compare the values of two ordinary variables, Var1 and Var2, we would just write:

However, the syntax for comparing two object variables to see if they refer to the same object is special (as is the syntax for setting the value of an object variable—using the Set statement). It is done using the Is operator:

Similarly, to test whether or not an object variable has been set to Nothing, we write: If rng Is Nothing Then . . .

Be advised that there is a problem with the Is operator in the current version of VBA. This problem exists in the version of VBA used by Office 97 and Office 2000. (Microsoft has acknowledged the problem.) For example, the code:

Dim Wks As Worksheet Dim Wks2 As Worksheet

Set Wks = ActiveSheet Set Wks2 = ActiveSheet

MsgBox Wks Is Wks2

will correctly display the value True. However, the analogous code:

Dim rng As Range Dim rng2 As Range

Set rng = ActiveSheet.Rows(1) Set rng2 = ActiveSheet.Rows(1)

MsgBox rng Is rng2

incorrectly displays the value False. If we change the penultimate line to:

Tea 97Fly®

Set rng2 = rng then the message box correctly displays True.

0 0

Post a comment