View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Robert Flanagan Robert Flanagan is offline
external usenet poster
 
Posts: 71
Default Why isn't range variable nothing in this case

Put the following code in a workbook:

Sub VariableTest()
Dim anyR As Range
Set anyR = Selection
anyR.Parent.Parent.Close False
If anyR Is Nothing Then
MsgBox "is nothing"
Else
MsgBox "is Not nothing"
End If
End Sub

Now go to another workbook and run it. Since the workbook which anyR
pointed to is now closed, I expected anyR to be nothing. But it is not.

What I want to do is to test if the workbook has been closed by seeing if
anyR is nothing. The only way I see is something like this:

dim wS as worksheet
On Error Resume next
set wS = nothing 'in case it has previously been set
set wS = anyR.Parent
On Error goto 0

I can then test if wS is nothing.

I would much prefer a test directly on the range variable without using an
error trap, as it is easy to forget to remove an error trap. And the above
is several lines of code (I can always put in a function) Is there a way to
do a direct test?

Bob