Greg Truby
MrExcel MVP
- Joined
- Jun 19, 2002
- Messages
- 10,030
Posting here 'cause more of a "I'm curious" question than an "I need help" question (though I'm sure Von Pookie will move me if I chose the wrong board)
My question: Why doesn't the IS operator return True when comparing Target to a range in VB when they are indeed the same? Why do we have to keep backing in via rng.Address = Target.Address or Not Intersect() Is Nothing?
Example:
<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_SelectionChange(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)
<SPAN style="color:#007F00">'_____Works_____</SPAN>
<SPAN style="color:#00007F">If</SPAN> Target.Address = "$B$2" <SPAN style="color:#00007F">Then</SPAN>
Application.StatusBar = "To Be"
<SPAN style="color:#00007F">Else</SPAN>
Application.StatusBar = "Not to be"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#007F00">'_____This fails_____</SPAN>
<SPAN style="color:#00007F">If</SPAN> Target <SPAN style="color:#00007F">Is</SPAN> Range("A1") <SPAN style="color:#00007F">Then</SPAN>
Application.StatusBar = "A1 sauce anyone?"
<SPAN style="color:#007F00">'____But this works_____</SPAN>
<SPAN style="color:#00007F">ElseIf</SPAN> <SPAN style="color:#00007F">Not</SPAN> Intersect(Target, Range("A1")) <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
Application.StatusBar = "¿Alguien quiere salsa A1?"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
Again - this is just me wondering why... Am I missing something terribly obvious here?
(Edit) I am guessing it has to do with the Target argument for SelectionChange() coming in ByVal instead of ByRef, but not sure... (End Edit)
Thanks for any insights.
Regards,

My question: Why doesn't the IS operator return True when comparing Target to a range in VB when they are indeed the same? Why do we have to keep backing in via rng.Address = Target.Address or Not Intersect() Is Nothing?
Example:
<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_SelectionChange(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)
<SPAN style="color:#007F00">'_____Works_____</SPAN>
<SPAN style="color:#00007F">If</SPAN> Target.Address = "$B$2" <SPAN style="color:#00007F">Then</SPAN>
Application.StatusBar = "To Be"
<SPAN style="color:#00007F">Else</SPAN>
Application.StatusBar = "Not to be"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#007F00">'_____This fails_____</SPAN>
<SPAN style="color:#00007F">If</SPAN> Target <SPAN style="color:#00007F">Is</SPAN> Range("A1") <SPAN style="color:#00007F">Then</SPAN>
Application.StatusBar = "A1 sauce anyone?"
<SPAN style="color:#007F00">'____But this works_____</SPAN>
<SPAN style="color:#00007F">ElseIf</SPAN> <SPAN style="color:#00007F">Not</SPAN> Intersect(Target, Range("A1")) <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
Application.StatusBar = "¿Alguien quiere salsa A1?"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
Again - this is just me wondering why... Am I missing something terribly obvious here?
(Edit) I am guessing it has to do with the Target argument for SelectionChange() coming in ByVal instead of ByRef, but not sure... (End Edit)
Thanks for any insights.
Regards,