+ Reply to Thread
Results 1 to 3 of 3

check if worksheet already exists when auto creating worksheets form a list

Hybrid View

  1. #1
    Registered User
    Join Date
    09-07-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    11

    check if worksheet already exists when auto creating worksheets form a list

    Hi everyone,

    I have a test file that creates new worksheets from a list.
    My problem is how do i check to see if the worksheet already exists?
    If it exists i want to skip that name on the list and continue down the list.

    any help is greatly appreciated.


    currently there are 3 worksheets
    template, List and Blank

    i'm using the following

    PHP Code: 
    Option Explicit
    Sub Add_NameWS2
    ()
    Dim myCell As Range
    With Worksheets
    ("List")
    For 
    Each myCell In .Range(("A1"), .Cells(.Rows.Count1).End(xlUp)).Cells
    Worksheets
    ("template").Copy after:=Worksheets(Worksheets.Count)
    On Error Resume Next
    ActiveSheet
    .Name myCell.Value
    Next myCell
    End With
    End Sub 

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: check if worksheet already exists when auto creating worksheets form a list

    Try this

    Option Explicit
    Sub Add_NameWS2()
    Dim myCell As Range
    Dim ValidName As Boolean
    With Worksheets("List")
    For Each myCell In .Range(("A1"), .Cells(.Rows.Count, 1).End(xlUp)).Cells
        ValidName = True
        Worksheets("template").Copy after:=Worksheets(Worksheets.Count)
        On Error GoTo NameAlreadyUsed
        ActiveSheet.Name = myCell.Value
        If ValidName = False Then
            Application.DisplayAlerts = False
            Worksheets(Worksheets.Count).Delete
            Application.DisplayAlerts = True
        End If
    Next myCell
    End With
    Exit Sub
    
    NameAlreadyUsed:
    ValidName = False
    Resume Next
    End Sub

  3. #3
    Registered User
    Join Date
    09-07-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: check if worksheet already exists when auto creating worksheets form a list

    mrice

    thanks that worked a treat 100% what i was trying to do.

+ 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