Does Worksheet Exist

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a quick and easy statement to use (in VBA) to check and see if a
particular worksheet exists (by name)?
 
-----------------------------------------------------------------
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)
 
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
 
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)
 

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

Back
Top