ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Error Routine (https://www.excelbanter.com/excel-programming/339996-error-routine.html)

Karen

Error Routine
 
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......

Norman Jones

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......





All times are GMT +1. The time now is 04:48 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com