Thread: Is Nothing
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_6_] Bob Phillips[_6_] is offline
external usenet poster
 
Posts: 11,272
Default Is Nothing

Not quite like that, but right principle

Dim sh As Worksheet

On Error Resume Next
Set sh = wkb.Sheets("TempSheet")
On Error Goto 0
If Not sh Is Nothing Then
wkb.Sheets("TempSheet").Delete
End If

This sets a worksheet variable to that worksheet, and tests if that object
variable is nothing. The Onerror Resume Next is just in case it does not
exist, then it resets.

You could always jus do

On Error Resume Next
wkb.Sheets("TempSheet").Delete
On Error Goto 0

on the basis that if you are going to delete it, no need to test.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"John" wrote in message
...
Hi there,

I'm always getting into trouble with the Is Nothing statement. At the
moment, I want to delete a specific sheet if it exists prior to adding
another of the same name, so:

If Not wkb.Sheets("TempSheet") Is Nothing Then
wkb.Sheets("TempSheet").Delete
End If

I'm getting a subscript out of range error on the first line (above) and a
err code of 9 (PS can anyone point me to a list of error codes?)

Can anyone tell me what I'm doing wrong?

Thanks

John