MORE INFORMATION
The "Is" function is used to compare two object reference variables. The
result of an Is operation is either TRUE (if the variables both refer to
the same object) or FALSE (if they do not).
For example, if you have the following code:
Set Alpha = ActiveWorkbook.Sheets(1)
Set Bravo = ActiveWorkbook.Sheets(1)
Set Charlie = Alpha
Set Delta = Alpha
All of these commands will return the value TRUE:
Alpha Is Charlie
Alpha Is Delta
Charlie Is Delta
Note that Bravo Is <object>, where <object> is Alpha, Charlie, or Delta,
will return FALSE, because the value of Bravo is not the same as the value
of Alpha, Charlie, or Delta, even though they both refer to the same
object.
If you use the "Is" function to compare a valid object reference (for
example, Alpha) to an invalid object reference (for example, a numeric
value, a string, or any other item that is not an object), you may receive
a GP fault.
To prevent this error from occurring, make sure both items being compared
are valid objects.
Visual Basic Code Example
Microsoft provides examples of Visual Basic for Applications procedures for
illustration only, without warranty either expressed or implied, including,
but not limited to the implied warranties of merchantability and/or fitness
for a particular purpose. The Visual Basic procedures in this article are
provided 'as is' and Microsoft does not guarantee that they can be used in
all situations. While Microsoft support professionals can help explain the
functionality of a particular macro, they will not modify these examples to
provide added functionality, nor will they help you construct macros to
meet your specific needs. If you have limited programming experience, you
may want to consult one of the Microsoft Solution Providers. Solution
Providers offer a wide range of fee-based services, including creating
custom macros. For more information about Microsoft Solution Providers,
call Microsoft Customer Information Service at (800) 426-9400.
The following code example illustrates the proper use of the "Is" function.
To run this example, position the cursor in the line that reads "Sub
GoodIsSubroutine()" and either press the F5 key or choose Start from the
Run menu.
'----------------------------------------------------------------------
Option Explicit
Sub GoodIsSubroutine()
'Dimension variables.
Dim Alpha As Variant, Bravo As Variant, Charlie As Variant
Dim Delta As Variant
'Create object names.
Set Alpha = ActiveWorkbook.Sheets(1)
Set Bravo = ActiveWorkbook.Sheets(1)
Set Charlie = Alpha
Set Delta = Alpha
'These are tests to see if one object Is another object. A message
'box is displayed if the objects are the same. In this case, only the
'first three messages will be shown.
If Alpha Is Charlie Then
MsgBox "Alpha is Charlie!"
End If
If Alpha Is Delta Then
MsgBox "Alpha is Delta!"
End If
If Charlie Is Delta Then
MsgBox "Charlie is Delta!"
End If
If Bravo Is Charlie Then
MsgBox "Bravo is Charlie!"
End If
If Bravo Is Delta Then
MsgBox "Bravo is Delta!"
End If
End Sub
'----------------------------------------------------------------------
When you run GoodIsSubroutine, you will be shown three message boxes,
one for each of the "Is" functions that succeeds.