Thread: Error Routine
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Norman Jones Norman Jones is offline
external usenet poster
 
Posts: 5,302
Default Error Routine

Hi Karen,

You could test for the existance of the sheet and skip the copy if the sheet
already exists.

Something like:

'================================
Sub Tester()
Dim res As String
res = InputBox("Please enter new sheet name")

If Not res = "" Then
If SheetExists(res) Then
'Skip copy
MsgBox "The " & res & " sheet already exists!"
Else
Sheets("Sheet1").Copy _
after:=ActiveWorkbook.Sheets(1)
ActiveSheet.Name = res
End If
Else
MsgBox "No name provided"
End If
End Sub

'---------------------------------

Function SheetExists(sName As String) As Boolean
On Error Resume Next
SheetExists = CBool(Len(Sheets(sName).Name))
On Error GoTo 0
End Function

'<<================================

---
Regards,
Norman



"Karen" wrote in message
...
I am sure this is a simply question...but my macro is copying an exisiting
worksheet and having the user enter a name. When the name is the same as
an
exisiting name, the user gets an error message, but the worksheet is still
created. What code will delete the worksheet if the name is wrong??

Thanks,
Karen......