Thread: Select Case
View Single Post
  #18   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Select Case

"The Is operator is an object reference comparison operator.

In a Select Case statement, the clause "Case Is" is not using the "Is"
operator. Think of "Case Is" as an single operation that just happens to use
the text "Is". If you do want to use the Is operator, you can do something
like

Dim R1 As Range
Dim R2 As Range
Dim X As Integer
Set R1 = Range("A1")
Set R2 = R1
X = 123
Select Case True
Case R1 Is R2 ' or (R1 Is R2) = True
Debug.Print "IS true"
Case Else
Debug.Print "Not IS true"
End Select



--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Bill Renaud" wrote in message
. ..
The last paragraph at the bottom of the "Operator Precedence" topic in
Visual Basic Help states:

"The Is operator is an object reference comparison operator. It does not
compare objects or their values; it checks only to determine if two object
references refer to the same object."

So, technically, I suppose it is not really correct programming to use the
Is operator when checking a variable that is a Variant, as in this case.

As Peter T mentioned, "Case Is = 2 Or 4" DOES WORK if x = 6! Your point
about it not throwing a compiler error is interesting, though. I noticed
that you had not declared the variable x in your sample code, so you must
not have used "Option Explicit" at the top of your code, which I assume
you
normally do. (I had to add a declaration when testing the version that I
tried.)

--
Regards,
Bill Renaud