ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   worksheets collection... find... (https://www.excelbanter.com/excel-programming/292301-worksheets-collection-find.html)

Mark Kubicki

worksheets collection... find...
 
what function do i use to determine if a worksheet name already exists;

or should i use error trapping to catch an attempt at creating a redundant
worksheet name?



Ron de Bruin

worksheets collection... find...
 
You can error trap or use a function

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


This in you macro

If SheetExists("Master") = True Then....


Or

Sub sheettest()
Dim N As Long
On Error Resume Next
With ThisWorkbook.Worksheets
N = Len(.Item("Report").Name)
If N = 0 Then
'your code
End If
End With
End Sub


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



"mark kubicki" wrote in message ...
what function do i use to determine if a worksheet name already exists;

or should i use error trapping to catch an attempt at creating a redundant
worksheet name?





Tom Ogilvy

worksheets collection... find...
 
proposedname = "Sheet1"
for each sh in worksheets
if lcase(sh.name) = lcase(proposedname) then
msgbox "Name is a duplicate"
exit for
end if
Next
End Sub

or you can error trap.

--
Regards,
Tom Ogilvy

"mark kubicki" wrote in message
...
what function do i use to determine if a worksheet name already exists;

or should i use error trapping to catch an attempt at creating a redundant
worksheet name?






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

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