View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default check if sheet exists

Ross,

To pass a workbook reference to the function, do something like

If SheetExists("Sheet123",Workbooks("Book2.xls")) = True Then

This will test whether Sheet123 exists in Book2, regardless of
what workbook is active or what workbook the code resides in.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com




"Ross" wrote in message
...
Thanks guys, I really appreciate the help. I was
wondering if you could provide me qith an example of
passing a workbook through the function as well. Thanks
again.


-----Original Message-----
Ross,

Try something like

Function SheetExists(SheetName As String, _
Optional Book As Workbook) As Boolean
Dim WB As Workbook
Dim N As Long
On Error Resume Next
If Book Is Nothing Then
Set WB = ThisWorkbook
End If
N = Len(WB.Worksheets(SheetName).Name)
SheetExists = (N < 0)
End Function

You can then call this with code like

If SheetExists("Sheet123") = True Then



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com





"Ross" wrote in message
...
how would you go about checking to see if a sheet exists
of not?



.