How to check if a sheet exists ?

  • Thread starter Thread starter Luc
  • Start date Start date
Courtexy Chip Pearson:

Function SheetExists(SheetName As String, _
Optional WB As Workbook) As Boolean
Dim W As Workbook
If WB Is Nothing Then
Set W = ActiveWorkbook
Else
Set W = WB
End If
On Error Resume Next
SheetExists = CBool(Len(W.Worksheets(SheetName).Name))
End Function




Title says it all.
Luc
 
Hi,

I'd have a seperate fumction and call it to test for the existance of a sheet

Sub nn()
MsgBox SheetThere("sheet3")
End Sub


Function SheetThere(ShtName As String) As Boolean
Dim x
On Error GoTo GetMeOut
x = ActiveWorkbook.Sheets(ShtName).Name
SheetThere = True
Exit Function

GetMeOut:
SheetThere = False
End Function


Mike
 
Here is a shorter version of your "on error" method for this function...

Function SheetThere(ShtName As String) As Boolean
On Error Resume Next
SheetThere = Len(ActiveWorkbook.Sheets(ShtName).Name)
End Function
 
Dude ! ! ! ! I LOVE YOU ! ! ! ! I Have searched over the internet for 3 hours.. Only yours worked the best.. Im going to share this to everyone..
 
Back
Top