Does Worksheet Exist

B

BillCPA

Is there a quick and easy statement to use (in VBA) to check and see if a
particular worksheet exists (by name)?
 
B

Bob Phillips

-----------------------------------------------------------------
Function SheetExists(Sh As String, _
Optional wb As Workbook) As Boolean
'-----------------------------------------------------------------
Dim oWs As Worksheet
If wb Is Nothing Then Set wb = ActiveWorkbook
On Error Resume Next
SheetExists = CBool(Not wb.Worksheets(Sh) Is Nothing)
On Error GoTo 0
End Function


--

HTH

RP
(remove nothere from the email address if mailing direct)
 
H

Harald Staff

It's prettty easy to write. Here's one solution:

Sub test()
MsgBox SheetXists("Sheet1")
MsgBox SheetXists("Rumsfeld Quotes")
End Sub

Function SheetXists(SheetName As String) As Boolean
On Error Resume Next
SheetXists = Len(Sheets(SheetName).Name)
End Function

HTH. Best wishes Harald
 
B

Bob Phillips

Same as sub, just call it differently.

If SheetExists("My Summary Page") Then
'do something
End If

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
Top