Does worksheet exist

J

John

Is it possible to make a conditional which determines if
a worksheet exists? This gives an error message so does
not work. Thanks

If IsNull(Worksheets("MySheet")) Then Debug.Print "Problem"
 
I

ivv

May be:On Error Goto ErrHandlerIf IsNull(Worksheets("MySheet")) Then
Debug.Print "Problem" ErrHandler:
 
D

Dick Kusleika

John

Here's a function that will do the job

Function SheetExists(sName As String, _
Optional oWb As Workbook) As Boolean

If oWb Is Nothing Then
Set oWb = ActiveWorkbook
End If

On Error Resume Next
SheetExists = CBool(Not oWb.Worksheets(sName) Is Nothing)
On Error GoTo 0

End Function
 
J

John

Thank you for the feedback.

John

Dick Kusleika said:
John

Here's a function that will do the job

Function SheetExists(sName As String, _
Optional oWb As Workbook) As Boolean

If oWb Is Nothing Then
Set oWb = ActiveWorkbook
End If

On Error Resume Next
SheetExists = CBool(Not oWb.Worksheets(sName) Is Nothing)
On Error GoTo 0

End Function


--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top