ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Naming Worksheets (https://www.excelbanter.com/excel-programming/335428-naming-worksheets.html)

Chaz

Naming Worksheets
 
I have a macro that creates and names a worksheet each time it is run.
However, if you have already run the macro and have not deleted the worksheet
before running the macro again, the macro returns an error - Cannot rename a
sheet to the same name as another sheet. What is the best way to handle this
error?

Thanks in advance

Tom Ogilvy

Naming Worksheets
 
Dim sh as Worksheet
On Error Resume Next
set sh = worksheets("Name1")
On Error goto 0
if not sh is nothing then
msgbox "Sheet with the name of Name1 already exists"
else
Worksheets.Add.Name = "Name1"
set sh = Worksheets("Name1")
End if

' now sh holds a reference to the sheet Name1

--
Regards,
Tom Ogilvy

"Chaz" wrote in message
...
I have a macro that creates and names a worksheet each time it is run.
However, if you have already run the macro and have not deleted the

worksheet
before running the macro again, the macro returns an error - Cannot rename

a
sheet to the same name as another sheet. What is the best way to handle

this
error?

Thanks in advance





All times are GMT +1. The time now is 06:24 AM.

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