+ Reply to Thread
Results 1 to 3 of 3

search for worksheet, insert new if doesn't exist

  1. #1
    Registered User
    Join Date
    03-02-2006
    Posts
    54

    search for worksheet, insert new if doesn't exist

    Im opening up a workbook and then selecting a worksheet based on a string variable like so (wsI is worksheet variable, sYear is search criteria)

    Set wsI = Sheets(sYear)

    I would like to improve this so that if the worksheet does not exist i insert a new one and then rename it to my search variable, i thought something along the lines of the below would work but it hasn't! I guess the 0 is the wrong thing to look for, any help appreciated

    If Sheets(sYear) = 0 Then
    Sheets.Add
    ActiveSheet.Name = sYear
    Else
    Set wsI = Sheets(sYear)
    End If

  2. #2
    Jim Thomlinson
    Guest

    RE: search for worksheet, insert new if doesn't exist

    Here is a function that tells you if a sheet exists in a given workbook

    Public Function SheetExists(SName As String, _
    Optional ByVal Wb As Workbook) As Boolean
    'Chip Pearson
    On Error Resume Next
    If Wb Is Nothing Then Set Wb = ThisWorkbook
    SheetExists = CBool(Len(Wb.Sheets(SName).Name))
    End Function

    If sheetexists(sYear) Then
    Set wsI = Sheets(sYear)
    Else
    Sheets.Add
    Set wsI = Activesheet
    wsI.Name = sYear
    End If

    --
    HTH...

    Jim Thomlinson


    "cereldine" wrote:

    >
    > Im opening up a workbook and then selecting a worksheet based on a
    > string variable like so (wsI is worksheet variable, sYear is search
    > criteria)
    >
    > Set wsI = Sheets(sYear)
    >
    > I would like to improve this so that if the worksheet does not exist i
    > insert a new one and then rename it to my search variable, i thought
    > something along the lines of the below would work but it hasn't! I
    > guess the 0 is the wrong thing to look for, any help appreciated
    >
    > If Sheets(sYear) = 0 Then
    > Sheets.Add
    > ActiveSheet.Name = sYear
    > Else
    > Set wsI = Sheets(sYear)
    > End If
    >
    >
    > --
    > cereldine
    > ------------------------------------------------------------------------
    > cereldine's Profile: http://www.excelforum.com/member.php...o&userid=32069
    > View this thread: http://www.excelforum.com/showthread...hreadid=535008
    >
    >


  3. #3
    Bob Phillips
    Guest

    Re: search for worksheet, insert new if doesn't exist

    On Error Resume Next
    Set wsI = Worksheets(sYear)
    On Error GoTo 0
    If wsI Is Nothing Then
    Worksheets.Add.Name = sYear
    Set wsI = Worksheets(sYear)
    End If

    --
    HTH

    Bob Phillips

    (remove nothere from email address if mailing direct)

    "cereldine" <[email protected]> wrote
    in message news:[email protected]...
    >
    > Im opening up a workbook and then selecting a worksheet based on a
    > string variable like so (wsI is worksheet variable, sYear is search
    > criteria)
    >
    > Set wsI = Sheets(sYear)
    >
    > I would like to improve this so that if the worksheet does not exist i
    > insert a new one and then rename it to my search variable, i thought
    > something along the lines of the below would work but it hasn't! I
    > guess the 0 is the wrong thing to look for, any help appreciated
    >
    > If Sheets(sYear) = 0 Then
    > Sheets.Add
    > ActiveSheet.Name = sYear
    > Else
    > Set wsI = Sheets(sYear)
    > End If
    >
    >
    > --
    > cereldine
    > ------------------------------------------------------------------------
    > cereldine's Profile:

    http://www.excelforum.com/member.php...o&userid=32069
    > View this thread: http://www.excelforum.com/showthread...hreadid=535008
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1