View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Tim Zych Tim Zych is offline
external usenet poster
 
Posts: 389
Default How do I test for the existance of a worksheet?

That's valid too Peter.

One thing which I had in mind doing it the long way was that if "Break On
All Errors" is checked, the shortcut way will halt on all instances where
the worksheet does not exist. If I'm debugging some code I might not want
the macro to halt in a helper function such as this. Practically a non-issue
99.9% of the time but it's something worth considering.

Regards,
--
Tim Zych
http://www.higherdata.com


wrote in message
...
Here's an example with the same way of thinking but with a bit simpler
implementation:

Function WsExists(wb As Workbook, strWsName As String) As Boolean
On Error Resume Next
WsExists = Not wb.Worksheets(strWsName) Is Nothing
End Function

best regard
Peder Schmedling

On Feb 3, 2:46 am, "Tim Zych" <feedback at higherdata dt com wrote:
Create a reusable function...you'll use it again and again..something
like:

Function WksExists(wkb As Workbook, WksName As String) As Boolean
Dim wks As Worksheet
WksExists = False
For Each wks In wkb.Worksheets
If wks.Name = WksName Then
WksExists = True
Exit Function
End If
Next
End Function

If WksExists(ThisWorkbook, "Sheet2") Then
'Code
End If

--
Tim Zychhttp://www.higherdata.com

"John" wrote in message

...

I have the name of a desired worksheet in the variable SheetName.


What form of an If statement do I use to execute some code if this
worksheet
exists?


I appreciate your help, -John