Re: How do I test for the existance of a worksheet?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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" <J...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message

news:FBF56BA5-14A7-4788-9B82-514A6FF24D1E@xxxxxxxxxxxxxxxx

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


.



Relevant Pages

  • Re: Template Issues from Staging Server to Production Server
    ... template to the Site Template Gallery, ... Callstack:    at ... strTitle, String strDescription, UInt32 nLCID, String strWebTemplate, ... Boolean bCreateUniqueSubweb, Boolean bConvertIfThere, Guid webId, Guid ...
    (microsoft.public.sharepoint.portalserver)
  • Re: 11-string recuerdos- where on the bell curve?
    ...   Yes, a couple of the ornaments in the first section did not come out ... which there is not room for improvement, slurs included, but I do not ... This is my recording of Recuerdos done on six string several years ... a heavier bass? ...
    (rec.music.classical.guitar)
  • Re: Mail Macro
    ... Dim sh As Worksheet ... Dim TempFilePath As String ... Dim SourceWB As Workbook ...     Dim sh As Worksheet ...
    (microsoft.public.excel.programming)
  • Re: Export multiple versions of a query to separate Excel files
    ... If I try to run the code I can select the folder that I want to create ... Dim qdf As DAO.QueryDef ... Dim strSQL As String, strTemp As String, strMgr As String ...
    (microsoft.public.access.externaldata)
  • Re: Using String for new List name
    ...     for line in infile: ... and aclS represents "access control list String." ... My quest, if you will, is to create a list of syslog entries, each ... Since ACL rules can be written with a range of granularity, ...
    (comp.lang.python)