View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Dan R. Dan R. is offline
external usenet poster
 
Posts: 220
Default On Error GoTo .... error

I'd just loop through all the sheets first to see if it exists:

Function myfunction()
Dim sh As Worksheet
Application.DisplayAlerts = False
For Each sh In ThisWorkbook.Worksheets
If sh.Name = "Working Template" Then
sh.Delete
Exit For
End If
Next sh

' do stuff

Application.DisplayAlerts = True
End Function

--
Dan

On Jan 3, 12:00*pm, wrote:
I have the following code which is supposed to enter a new worksheet,
rename it "Working Template" and do other stuff. *There is an error
check that is supposed to flag the error that occurs if a "Working
Template" sheet already exists - then delete the current (newly added)
sheet and the "Working Template", then go back to the beginning. *For
some reason, though, it's just not working at all. *I wrote the code
on another computer and had no problems, but on this machine it
refuses to catch it. *I could rewrite it to circumvent the problem,
but it is happening elsewhere too, so i would like to get a better
understanding of why.

Any advice or recommendations would be greatly appreciated.

Function ()
beginning:
* * Sheets.Add After:=Sheets(6)
* * On Error GoTo error_occured
* * Sheets(7).Name = "Working Template"
* * Sheets(6).Cells.Copy
* * Sheets(7).Select
* * With Selection
* * * * .PasteSpecial Paste:=xlPasteValues
* * * * .PasteSpecial Paste:=xlPasteFormats
* * End With
* * Cells.UnMerge
Exit Function

error_occured:
* * * * Application.DisplayAlerts = False
* * * * * * ActiveSheet.Delete
* * * * * * Sheets("Working Template").Delete
* * * * Application.DisplayAlerts = True
* * * * Resume beginning
End Function

Thanks,
Thedude