How to check if a sheet exists ?

J

JLGWhiz

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
 
M

Mike H

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
 
R

Rick Rothstein

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
 
V

Vivek Samapra

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..
 

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