Why does Target Is Range("A1") fail?

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,025
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) :coffee:

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,
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Every object has a default property. The default property of the Range object is "Value". This is an inherent thingamabob and I can't remember if or how you can change this.

So the line

Code:
  If Target Is Range("A1")

is actually the equivalent to saying:

Code:
  If Target Is Range("A1").Value

...which is not comparing the objects, but rather comparing the Target object to the Value of Range A1.

I would like to illustrate this with an example, but I can't right now, so this is just my theory until I can disprove it. My main stumbling block is that the Target object is also a range.
 
Upvote 0
Hello,

Greg Truby said:
Again - this is just me wondering why... Am I missing something terribly obvious here?
No, as far as I can discern, the is operator should be functional and accurate here, but for some opaque reason, it is not with range objects, I do not know why.

See the following:

<font face=Courier New><SPAN style="color:darkblue">Sub</SPAN> test1()
<SPAN style="color:darkblue">Dim</SPAN> y <SPAN style="color:darkblue">As</SPAN> Range, z <SPAN style="color:darkblue">As</SPAN> Range
<SPAN style="color:darkblue">Set</SPAN> y = Range("a1")
<SPAN style="color:darkblue">Set</SPAN> z = Range("a1")
MsgBox y <SPAN style="color:darkblue">Is</SPAN> z
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN>


<SPAN style="color:darkblue">Sub</SPAN> test2()
<SPAN style="color:darkblue">Dim</SPAN> y <SPAN style="color:darkblue">As</SPAN> Worksheet, z <SPAN style="color:darkblue">As</SPAN> Worksheet
<SPAN style="color:darkblue">Set</SPAN> y = Sheets(2)
<SPAN style="color:darkblue">Set</SPAN> z = Sheets(2)
MsgBox y <SPAN style="color:darkblue">Is</SPAN> z
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>

Fine with worksheets, not so good with ranges. :unsure:

Incidentally, while I don't have a great answer, I do believe this is a legitimate Excel VBA question, I shall move it to the Q&A forum.

Edit: Looks like it's a bug:
http://support.microsoft.com/default.aspx?scid=kb;en-us;114345&Product=xlw
 
Upvote 0
Mark,

If the default property was getting in the way, then wouldn't the following also fail?

<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:#00007F">Dim</SPAN> rng1 <SPAN style="color:#00007F">As</SPAN> Range
    <SPAN style="color:#00007F">Set</SPAN> rng1 = Target
    <SPAN style="color:#007F00">'_____Returns TRUE_____</SPAN>
    <SPAN style="color:#00007F">If</SPAN> rng1 <SPAN style="color:#00007F">Is</SPAN> Target <SPAN style="color:#00007F">Then</SPAN> MsgBox "True" <SPAN style="color:#00007F">Else</SPAN> MsgBox "False"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Nate,

Thanks for the reference. Interestly, the code that they provide as a test to "replicate" the problem does produce a true (it beeps). I tried using the generic Object type, didn't work:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> TestAsObject()
    <SPAN style="color:#00007F">Dim</SPAN> y <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, z <SPAN style="color:#00007F">As</SPAN> Object
    
    <SPAN style="color:#00007F">Set</SPAN> y = Range("a1:b2")
    <SPAN style="color:#00007F">Set</SPAN> z = Range("a1:b2")
    MsgBox y <SPAN style="color:#00007F">Is</SPAN> z, , "Ranges"           <SPAN style="color:#007F00">'____False</SPAN>

    <SPAN style="color:#00007F">Set</SPAN> z = y
    MsgBox y <SPAN style="color:#00007F">Is</SPAN> z, , "Ranges 2"         <SPAN style="color:#007F00">'____True</SPAN>

    <SPAN style="color:#00007F">Set</SPAN> y = ActiveSheet.Columns(1)
    <SPAN style="color:#00007F">Set</SPAN> z = ActiveSheet.Columns(1)
    MsgBox y <SPAN style="color:#00007F">Is</SPAN> z, , "Columns"          <SPAN style="color:#007F00">'____False</SPAN>

    <SPAN style="color:#00007F">Set</SPAN> y = Sheets(2)
    <SPAN style="color:#00007F">Set</SPAN> z = Sheets(2)
    MsgBox y <SPAN style="color:#00007F">Is</SPAN> z, , "Sheets"           <SPAN style="color:#007F00">'____True</SPAN>

    <SPAN style="color:#00007F">Set</SPAN> y = Workbooks(2)
    <SPAN style="color:#00007F">Set</SPAN> z = Workbooks(2)
    MsgBox y <SPAN style="color:#00007F">Is</SPAN> z, , "Workbooks"    <SPAN style="color:#007F00">'____True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

And my original theory of because it's passed byVal doesn't appear to hold water either...

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> TestPassing()
    <SPAN style="color:#00007F">Dim</SPAN> rng1 <SPAN style="color:#00007F">As</SPAN> Range, ws1 <SPAN style="color:#00007F">As</SPAN> Worksheet, wb1 <SPAN style="color:#00007F">As</SPAN> Workbook
    <SPAN style="color:#00007F">Set</SPAN> rng1 = [A1]
    <SPAN style="color:#00007F">Set</SPAN> ws1 = Sheets(1)
    <SPAN style="color:#00007F">Set</SPAN> wb1 = Workbooks(1)
    TestCatching rng1, ws1, wb1
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> TestCatching(<SPAN style="color:#00007F">ByVal</SPAN> rngA <SPAN style="color:#00007F">As</SPAN> Range, _
                                <SPAN style="color:#00007F">ByVal</SPAN> wsA <SPAN style="color:#00007F">As</SPAN> Worksheet, _
                                <SPAN style="color:#00007F">ByVal</SPAN> wbA <SPAN style="color:#00007F">As</SPAN> Workbook)
                                
    <SPAN style="color:#00007F">Dim</SPAN> rng2 <SPAN style="color:#00007F">As</SPAN> Range, ws2 <SPAN style="color:#00007F">As</SPAN> Worksheet, wb2 <SPAN style="color:#00007F">As</SPAN> Workbook
    <SPAN style="color:#00007F">Set</SPAN> rng2 = [A1]
    <SPAN style="color:#00007F">Set</SPAN> ws2 = Sheets(1)
    <SPAN style="color:#00007F">Set</SPAN> wb2 = Workbooks(1)
    MsgBox rngA <SPAN style="color:#00007F">Is</SPAN> rng2, , "Ranges byVal"           <SPAN style="color:#007F00">'____Returns FALSE</SPAN>
    MsgBox wsA <SPAN style="color:#00007F">Is</SPAN> ws2, , "Worksheets byVal"     <SPAN style="color:#007F00">'____Returns TRUE</SPAN>
    MsgBox wbA <SPAN style="color:#00007F">Is</SPAN> wb2, , "Workbooks byVal"      <SPAN style="color:#007F00">'____Returns TRUE</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

So, mark me as "plumb stumped". But glad to know that it's a stumper of a question!
 
Upvote 0
Greg Truby said:
Interestly, the code that they provide as a test to "replicate" the problem does produce a true (it beeps). I tried using the generic Object type, didn't work:

This is an interesting bug. I see the article is quite old - refers to VBA ver 1 and Excel 5 so MS has obviously fixed something since then, but what exactly?
 
Upvote 0
I just spent half an hour searching Google and MSDN and couldn't find anything useful, just this acknowledgement of the problem.

Anyone else?
 
Upvote 0
Thanks for the "heads up" with that link, Nate. Through experience I'd learned that the IS operator wouldn't work well on range objects (but thought it was just me - hence my post on this). But I had no idea it could fail on other objects too. That's a nice pitfall to know about. It hasn't happened, but I could easily see myself spenting a good long time doin' this number :oops: in a debugging quagmire at some future date if not for your input!

Thanks again!

Greg
 
Upvote 0
You're welcome Greg. :)

The stated cause in the second reference may indeed be true, but it doesn't go far enough. In my first example, I wasn't calling properties separately, I had two fixed variables which should be static at that point, I haven't tested it, but I expect no better results with Constants... It simply leads me to believe:

1) The underlying issue with Excel is deeper than the stated cause with respect to ranges.
2) You're rolling the dice when you use the Is Operator{/i] on Excel objects. Although, I've never had a problem with:

test = object is nothing

Interesting question. Bon chance. :)
 
Upvote 0
Greg Truby said:
Mark,

If the default property was getting in the way, then wouldn't the following also fail?

<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:#00007F">Dim</SPAN> rng1 <SPAN style="color:#00007F">As</SPAN> Range
    <SPAN style="color:#00007F">Set</SPAN> rng1 = Target
    <SPAN style="color:#007F00">'_____Returns TRUE_____</SPAN>
    <SPAN style="color:#00007F">If</SPAN> rng1 <SPAN style="color:#00007F">Is</SPAN> Target <SPAN style="color:#00007F">Then</SPAN> MsgBox "True" <SPAN style="color:#00007F">Else</SPAN> MsgBox "False"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Yes, hence the part of my original post:

Mark O'Brien said:
I would like to illustrate this with an example, but I can't right now, so this is just my theory until I can disprove it. My main stumbling block is that the Target object is also a range.

Thanks for the links Nate.
 
Upvote 0

Forum statistics

Threads
1,216,109
Messages
6,128,880
Members
449,477
Latest member
panjongshing

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top