ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   checking for duplicate worksheets (https://www.excelbanter.com/excel-programming/288786-checking-duplicate-worksheets.html)

Rick B[_6_]

checking for duplicate worksheets
 
I have written a little VBA to create a new worksheet tab. The naming of
this tab is obtained from a specific cell. If a worksheet name already
matches data that is entered into that cell, an error occurs. How do I first
check if the worksheet name does not already exist before trying to rename
the new sheet?

Sheets("Template").Select
Sheets("Template").Copy After:=Sheets(2)
Sheets("Template (2)").Select
ActiveSheet.Name = Sheets("StartHere").Range("B3")

Thanks in advance,

Rick



Ron de Bruin

checking for duplicate worksheets
 
Hi Rick

You can trap the error like this

On Error Resume Next
ActiveSheet.Name = Sheets("StartHere").Range("B3")
On Error GoTo 0

Or use a function to check if the sheet exist

Function SheetExists(SName As String, _
Optional ByVal WB As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If WB Is Nothing Then Set WB = ThisWorkbook
SheetExists = CBool(Len(WB.Sheets(SName).Name))
End Function

You can use this line then

If SheetExists("test") = True Then


--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2000-2003)
www.rondebruin.nl



"Rick B" wrote in message ...
I have written a little VBA to create a new worksheet tab. The naming of
this tab is obtained from a specific cell. If a worksheet name already
matches data that is entered into that cell, an error occurs. How do I first
check if the worksheet name does not already exist before trying to rename
the new sheet?

Sheets("Template").Select
Sheets("Template").Copy After:=Sheets(2)
Sheets("Template (2)").Select
ActiveSheet.Name = Sheets("StartHere").Range("B3")

Thanks in advance,

Rick






All times are GMT +1. The time now is 07:37 PM.

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